Bhubaneswar, Odisha, India
+91-8328865778
support@softchief.com

Sample Synchronus & Asynchronous Codes For Dataverse Model Driven App to Enable/Disable Command Bar Button in Model Driven App Power Apps

Sample Synchronus & Asynchronous Codes For Dataverse Model Driven App to Enable/Disable Command Bar Button in Model Driven App Power Apps

Lets take a scenario on Command Button Enable Rule.

Here is a Synchronus code.

// NOTE: Don't do this!
function EnableRule() {
    const request = new XMLHttpRequest();

    // Pass false for the async argument to force synchronous request
    request.open('GET', '/bar/foo', false);
    request.send(null);
    return request.status === 200 && request.responseText === "true";
} 

Here is the Asynchronus relevant code for above using Promises. before using this make sure to enablke the feature for your Model driven app for Async OnSave, Async OnLoad. Here we ae wrapping the request handlers in a Promise and resolving or rejecting when the request is finished, the rule will not block the browser from performing more work.

function EnableRule() {
    const request = new XMLHttpRequest();
    request.open('GET', '/bar/foo');
    return new Promise(function(resolve, reject) {
        request.onload = function (e) {
            if (request.readyState === 4) {
                if (request.status === 200) {
                    resolve(request.responseText === "true");
                } else {
                    reject(request.statusText);
                }
            }
        };
        request.onerror = function (e) {
            reject(request.statusText);
        };
        request.send(null);
    });
}