Business Applications, dynamics 365, Dynamics 365 - new UI, PowerApps

How do I pass executionContext as a parameter into my Promise to fit in Promise.all()?

To be honest, this post should be a bit longer with more details but I like it this way.

This is a sample below. It looks cool. And I building one of my own. But I need it to work with my onSave form validation.

const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then((values) => {
  console.log(values);
});

I am making two Web API calls to query data to enforce some business rules. They have to be wrapped into Promise.all as per docs:

https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/events/form-onsave

I don’t normally use OnSave validation but unfortunately it’s required for my scenario.

I need to pass executionContext parameter into my Promise. And I do it like this:

const validateRateCard = context => {
        return new Promise(function (resolve, reject) {
            var formContext = context.getFormContext();

            var field = formContext.getAttribute("field_name").getValue();           
           
            var url = "<your_query_here>";
            Xrm.WebApi.retrieveMultipleRecords("<your_table_here", url).then(
                function success(result) {
                    // return true or false
                    if (result.entities.length > 0) {
                        resolve("NOT SO GOOD!");
                    }
                    else {
                        resolve("AWESOME COOKIES!");
                    }
                },
                function (error) {
                    reject(error.message);
                    console.log(error.message);
                }
            );
        })
    }        

The cool stuff that you could find the info on how to pass a parameter by Googling it. And the answer will always be “wrap your Promise into a function, pass the parameter into the function”.

It wasn’t clear still how to call that function and how to pass all this “celebration” all together into Promise.all the way it looks appropriate.

Promise.all([validateRateCard(executionContext), promise2]).then((values) => {
           
            for (const element of values) {
               
//lets iterate through the results
            }
           
        })
;

The absolute MUST reading if you play with OnSave validation is the article from Andrew Butenko:

https://butenko.pro/2018/11/15/cancelling-save-based-on-the-result-of-async-operation/

Good luck! You need it.

1 thought on “How do I pass executionContext as a parameter into my Promise to fit in Promise.all()?”

Leave a Reply

Fill in your details below or click an icon to log in:

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