Wednesday, April 3, 2024

Dynamics 365 CE: Call Xrm.WebApi using asynchronous call (async and await)

The following code can be used to call Xrm.Webapi using async and await.


function assignIndustryToContact(executionContext){
    var formContext = executionContext.getFormContext();    
    var accountId = formContext.getAttribute("parentcustomerid").getValue();

    if (accountId != null){
        getIndustryName(executionContext);              
    }
}

async function getIndustryName(executionContext){
    var formContext = executionContext.getFormContext();  
    var accountId = formContext.getAttribute("parentcustomerid").getValue();
    var accountIdGUID = accountId[0].id;    
    let industry = await Xrm.WebApi.retrieveRecord("account", accountIdGUID, "?$select=name,industryid");  
     
    if (industry._igl_industryid_value != null){
        let industryName = await Xrm.WebApi.retrieveRecord("igl_industry", industry.industryid, "?$select=name");
        formContext.getAttribute("fieldName").setValue(industryName.name);
    }  
}


Regards,

Arun S Keerthi

No comments:

Post a Comment