Update OptionSet value of related entity on change of another OptionSet(Parent entity) using Web API JavaScript

How to update OptionSet value of related entity on change of another OptionSet value using Web API – JavaScript. Or How to update subgrid field values using Web API(client side java script).

I need parent entity optionset value on child entity when it change. As we are aware that we can copy field value to related entity using “field mapping” but the case where field value change frequently then “field mapping” will not work – for example – Status field. Suppose, I have an optionset for status field which change frequently and it’s normal.

There are two ways to achieve it – One is to write plugin which will change optionset value of child entity based on parent entity optionset value change and two – write JS using Web API to update related entity field.

function updateOptionSetValue() {
var status = Xrm.Page.getAttribute(“statusfieldschemaname”).getValue();
if (status != “”) {
var rowCount = Xrm.Page.getControl(“subgridname”).getGrid().getTotalRecordCount();
for (var i = 0; i < rowCount; i++) { //Read last record values
var allRows = Xrm.Page.getControl(“subgridname”).getGrid().getRows();
var allGridRowData = [];
allRows.forEach(function (row, i) {

//Read values of all the child grid fields and update Status value
for (var i = 0; i < allGridRowData.length; i++) {
var Name = (allGridRowData[i].getEntity().getAttributes().getByName(“name”).getValue());
if (Name != null) {
var recordId = Xrm.Page.getControl(“subgridname”).getGrid().getRows().get(i).getData().getEntity().getId();
var ID = recordId.replace(/\{|\}/gi, “”);
var data = {
“value”: status

var req = new XMLHttpRequest();
req.open(“PATCH”, Xrm.Page.context.getClientUrl() + “/api/data/v8.2/entityschemaname(” + ID + “)/relatedentityoptionsetschemaname”, true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
//Xrm.Utility.alertDialog(‘Record Updated Successfully.’);
} else {

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s