This is a recent challenge came on my way. I had to refresh a sub grid of a form after an operation (say a field value change or Save). Challenge is Sub Grid values are being modified asynchronously through a server side logic. In summery, I have to delay the refresh till that happens.
If I elaborate this further, I have a field (i.e. Amount) in the form of Service entity. Once it is updated the server side logic will trigger and synchronously populate a field (i.e. Item Amount) of the child Service Item entity, which is the child record type of the grid.
While we need to run this Java Script on Save. Below is the code in my on Save script and I will explain each of the pieces come with the functions being called.
var formContext = executionContext.getFormContext(); var dirtyAttributes = CheckDirtyFieldsOnForm(formContext); if (dirtyAttributes != null && dirtyAttributes.length > 0 && dirtyAttributes.includes("new_amount") && formContext.getAttribute("new_amount") != null && formContext.getAttribute("new_amount").getValue() > 0) { var childId = RetrieveChildRecordToBeUpdate(formContext); if (childId != null) { IsServiceItemAmountSet(formContext, childId, 1);
} }
function CheckDirtyFieldsOnForm(formContext) { var attributes = formContext.data.entity.attributes.get(); var dirtyAttributes = []; if (attributes != null) { for (var i in attributes) { if (attributes[i].getIsDirty()) { dirtyAttributes.push(attributes[i].getName()); } } } return dirtyAttributes; }
function RetrieveChildRecordToBeUpdate(formContext) { var gridContext = formContext.getControl("grid_serviceitems"); if (gridContext == null || gridContext == "undefined") return; var complGrid = gridContext.getGrid(); var complRows = complGrid.getRows(); var rowCount = complRows.getLength(); if (rowCount == 0) { return; } for (var i = 0; i < rowCount; i++) { var rowEntity = complRows.get(i).getData().getEntity(); // Can add some logic if you have filter criteria for // select line item return rowEntity._entityId.guid; } return; }
function IsServiceItemAmountSet(formContext, Id, index) { Xrm.WebApi.online.retrieveRecord("new_serviceitem", Id, "?$select=new_itemamount").then( function success(result) { if (result.new_lineamount != null && result.new_lineamount > 0) { formContext.getControl("grid_serviceitems").refresh(); return true; } else { return false; } }, function (error) { } ).then(function (isSuccess) { if (!isSuccess && index < 5) { index++; setTimeout(function () { IsChildClaimAmountSet(formContext, Id, index); }, 2000); } }); }
No comments:
Post a Comment