Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

How to get async await to work with multiple functions?

Posted on by 190

I have a web resource using javascript and it worked using XMLHttpRequest(), but i know it is deprecated so i wanted to convert to Xrm.WebApi.  

The way i had it, it seems like my power automate flow is not picking up the value quick enough the first time, but on 2nd attempts it gets it.  I need to be more consistent so I decided to use async/await but it is still not working.  I think my issue is with passing value from one function and using it in another function.  These passed values will be used as API call filters.  Here is my structure:

var varA, varB, varC;

async function mainFunction(executionContext) {

var formContext = executionContext.getFormContext();
varA = formContext.data.entity.attributes.get("control1");
.......
........
varB = varA.getValue()[0].name;
await apiCall1(formContext);
await apiCall2(formContext);
}
------------------------------------------------------------------------------
async function apiCall1(formContext) {
await Xrm.WebApi.retrieveMultipleRecords("table1", "?$select=pcc_email, pcc_purchaserequestusersid" +  "&$filter=" + "pcc_name" + " eq " + "'" + varB + "'").then(
        function success(result) {
.................
..................
varC = result.entities[0]["pcc_email"];

formContext.getAttribute("pcc_vpemailaddress").setValue(emp_email);
.................
................
}
-------------------------------------------------------------------------------
async function apiCall2(formContext) {

   

await Xrm.WebApi.retrieveMultipleRecords("table2", "?$select=systemuserid, fullname, internalemailaddress" +  "&$filter=" + "internalemailaddress" + " eq " + "'" + varC + "'").then(
function success(result) {
.............................
var sys_Vplookup = new Array()
.............................
formContext.getAttribute("pcc_vpsystemuser").setValue(sys_Vplookup);
}


  • bp3378 Profile Picture
    bp3378 190 on at
    RE: How to get async await to work with multiple functions?

    a33ik Albion Ferracaku

    Thanks to both of your help on this.  I have moved on to an alternative and probably the more logical solution.  Instead of loading script on save, i am loading on change, then the user will save afterwards.  

    As for why XMLHttpRequest saves and xrm,webapi does not, i still don't know why but Albion your explanation makes a lot of sense to me.  Thank you both!

  • Suggested answer
    Albion Ferracaku Profile Picture
    Albion Ferracaku 20 on at
    RE: How to get async await to work with multiple functions?

    The reason why XMLHttpRequest seems "faster" than Xrm.WebApi can be the fact that you are making sync requests with XMLHttpRequest (third parameter at req.open set to false).

    Does that web resource prevents saving of form?

    So what happens maybe is something like: user saves the form, saving "stopped", doing some sync work(setting of lookup field), saving continue --> done.

    When you convert code to use WebApi: the sync job is converted to async. Async work is not blocking by nature and this means that flow becomes something like: user saves the form, saving "stopped", saving continue (async work not started yet) --> done, async work waiting in event loop queue to be executed.

    If you change the third parameter of req.open from false to true I expect that you see the same behavior as Xrm.WebApi

    Hope it helps!

    Albi

  • bp3378 Profile Picture
    bp3378 190 on at
    RE: How to get async await to work with multiple functions?

    NA

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to get async await to work with multiple functions?

    There should be no difference between XMLHttpRequest and Xrm.WebApi.

    Please provide the full versions of your original code and the modified one.

  • bp3378 Profile Picture
    bp3378 190 on at
    RE: How to get async await to work with multiple functions?

    a33ik

    I tried your suggestion but the value of varC is only calculated after the api call using varB.  So with putting two api calls in an array, only the first would return an object but the second would be undefined.  

    I did a test to just try one api call, the one using varB.  When i save the form it triggers the script which in turn loads the value in the field in my form, but when i do a refresh on the form, that field is still blank.  It apparently doesnt save it.  

    But if i use XMLHttpRequest() instead, it saves it for some reason.  What can I do with xrm.webapi to load the value in the field AND save it as well?  

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to get async await to work with multiple functions?

    Aha. Ok.

    You can try the following - I used it in multiple cases.

    var promices = [Xrm.WebApi.retrieveMultipleRecords("table1", "?$select=pcc_email, pcc_purchaserequestusersid" +  "&$filter=" + "pcc_name" + " eq " + "'" + varB + "'"),

    Xrm.WebApi.retrieveMultipleRecords("table2", "?$select=systemuserid, fullname, internalemailaddress" +  "&$filter=" + "internalemailaddress" + " eq " + "'" + varC + "'")];

    var promicesResults = await Promice.all(promices);

    var promise1Result = promicesResults[0];

    var promise2Result = promicesResults[1];

    //Add the code to handle both results the same time

  • bp3378 Profile Picture
    bp3378 190 on at
    RE: How to get async await to work with multiple functions?

    a33ik

    In my app, the user selects an approver from a lookup field that lookups to a custom user table.  When user saves, the web resource is triggered.  My script grabs the approver from the lookup field, does an api call to get the email address of that approver from the custom user table.  Then using this email address, i'm making a 2nd api call, this time to the systemuser table to get the approver's userid and fullname.   I use this to populate the approver(systemuser) lookup field.  

    Then i have a power automate flow that is triggered when row is added/modified based on that custom approver lookup field.  The flow uses the approver(systemuser) field to do something.  

    I think i know what is the problem now but havnt found a solution yet.   For some reason when i use XMLHttpRequest(); to make the api call, the approver(systemuser) field is saved at the same time approver(custom user) is saved.  But when i use Xrm.Web Api to make the api call, approver(systemuser) is not saved at the same time approver(custom user) is saved.   And so when the user selects the approver from the custom user table then saves, the flow see's the approver(systemuser) field when using XMLHttpRequest, but does not see it when using Xrm.Web..

    I dont see any save code in the script any where so i do not know why that's the case.   Unless XMLHttpRequest just executes way faster, and it gets, then populates the approver(systemuser) fast enough, before the record is actually saved........

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to get async await to work with multiple functions?

    Hello,

    I believe that the nature of your issue is slightly different and related to Async Nature of Power Automate. Can you please describe scenario you try to implement?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans