web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

JS - control.setDisabled() not working in Promise/Asynchronous function

(0) ShareShare
ReportReport
Posted on by 60

Hey all,

I have the following code which will lock all fields on the selected row of an editable subgrid - this works fine:

function onGridRowSelected(context){
    context.
        getFormContext().
        getData().
        getEntity().
        attributes.
        forEach(function (attr) {
            attr.controls.forEach(function (myField) {
                myField.setDisabled(true);
            });
        });
}

When I try to call this function from the response of an async function, nothing happens (my breakpoint shows that it is setting the control to locked, yet it does not happen on the form).

In the below code, I fetch a row using retrieveMultipleRecords(). I await this in an async function and use the return of the Promise to call my setFieldLockedProperty() function.

var gridContext;
function onGridRowSelected(context){
    gridContext = context.getFormContext();
    Xrm.WebApi.retrieveMultipleRecords("ms_approvalquery", "?$select=ms_responsetext&$top=1&$orderby=createdon desc")
    .then(result => lockOrUnlock(result));
}


function lockOrUnlock(result){
    var foundResponse = false
    if (result.entities[0].pa_responsetext != null){
        foundResponse = true;
    }

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

Could someone please explain why control.setDisabled() will not work if done on an async thread? Are there any workarounds for this? I've refactored my code several time to try different methods however I have come to a dead end.

Thanks!

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hi Gregg,

    I have tried many ways. However, get the same result with yours. It seems that setDisabled() function can't work in async method.

    You would better raise a ticket to feedback this. 

  • Gregg Profile Picture
    60 on at

    Thanks Steve, I used XMLHttpRequest() synchronously as a workaround.

  • Community Member Profile Picture
    on at

    Hi Gregg,

    Thanks for sharing your solution.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 60 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 51

#3
Satyam Prakash Profile Picture

Satyam Prakash 42

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans