Search code examples
javascriptpromisedynamics-crmes6-promisedynamics-365

Using control.setDisabled() after promise


I'm writing some JS for Dynamics 365 which disables (locks) the fields on the selected editable subgrid row.

The method to do this is .setDisabled() (Documentation). I can run the following method which will lock all the fields upon selecting a row:

function onGridRowSelected(context){
    context.data.entity.attributes.forEach(function (attr) {
        attr.controls.forEach(function (myField) {
            myField.setDisabled(foundResponse);
        })
    });
}

The issue I am having is trying to run the above following a promise. I have the following code which will pass the result of a promise into my disable fields methods:

var gridContext;
function onGridRowSelected(context){
    gridContext = context.getFormContext();

    //Retrieve the record we want to check the value on
    Xrm.WebApi.retrieveMultipleRecords("ms_approvalquery", "?$select=ms_responsetext&$top=1&$orderby=createdon desc")
    .then(result => disableOrEnableFields(result));
}


function disableOrEnableFields(result){
    //Check if the record found has a ms_responsetext != null
    var foundResponse = false
    if (result.entities[0].ms_responsetext != null){
        foundResponse = true;
    }

    //Either disable/enable all the row columns depending on the value retrieved from the above
    gridContext.data.entity.attributes.forEach(function (attr) {
        attr.controls.forEach(function (myField) {
            myField.setDisabled(foundResponse);
        })
    });
}

When stepping through debug, I can see that myField.setDisabled(true); is getting called but nothing is happening. Is this because it's on a separate thread? How do I get back to the main thread with the result of my promise?

Note: Using Async/Await doesn't work either - it gives the same results.


Solution

  • we had similar issues few days back, unfortunately Async/Await/promise call does not respect grid control, you will have to go by old/classic Sync call way and then it shall work. Let me know if this solves your problem.