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);
}