web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Unanswered

The return value from a Asynchronous Xrm.WebApi is always undefined

(0) ShareShare
ReportReport
Posted on by 5

Hello Team, 

I am working with the CRM D365, Where I am working on a button, I have written the code using asynchronous call Xrm.WebApi to run synchronously that I was able to manage using the asycn/await. I am having a problem when I return a Boolean value from the code into another function. Where it always returns promise pending, but in the results I can see the value. but I am unable to store that value in a local variable in the other function. Please find my code.

As said I am getting the true/false value within the results of the function : validationForPackagingStructure but the value is not getting stored inside the GlobalNullCheck variable. When I try to rerun this value its promise pending or undefined

I want to return this value from this function as i have other validation based on this return value. Any help here

var PromiseFunction;
if (PromiseFunction === undefined || PromiseFunction === null) {
PromiseFunction = {};
PromiseFunction = (function () {
var GlobalNullCheck;

var globalNullCheckFunction = function (formContext) {
var formContext = formContext;
var Flag = returnValueFunction(formContext);
return Flag;
}

var returnValueFunction = function (formContext) {
var formContext = formContext;
try {
validationForPackagingStructure(formContext).then(results => {
GlobalNullCheck = results;
return GlobalNullCheck;
});
}
catch {
console.log("Error");
}
};

var validationForPackagingStructure = async function (formContext) {
var formContext = formContext;
var CurrentRecordId = formContext.data.entity.getId().replace('{', '').replace('}', '');
var CurrentEntityName = formContext.data.entity.getEntityName();
var nullCheckFalg = false;
try {
var marketingResults = await Xrm.WebApi.retrieveRecord(CurrentEntityName, CurrentRecordId, "?$select=_new_marketing_value&$expand=new_Marketing($select=new_requesttype)");
var RequestType = marketingResults.new_Marketing.new_requesttype;
var uOmResults = await Xrm.WebApi.retrieveMultipleRecords("new_unitofmeasure", "?$select=_new_pum_value&$expand=new_new_unitofmeasure_new_fileattachments($select=new_name)&$filter=_new_pum_value eq " + CurrentRecordId);
for (var i = 0; i < uOmResults.entities.length; i++) {
var NumberOfAttachments = uOmResults.entities[0].new_new_unitofmeasure_new_fileattachments.length;
if (RequestType == 100000000 || RequestType == 100000001) {
if (NumberOfAttachments < 1) {
var message = "Packaging Structure Inputs records should contain at least one Attachment";
var level = "ERROR";
var uniqueId = "PackAttachID";
formContext.ui.setFormNotification(message, level, uniqueId);
nullCheckFalg = true;
return nullCheckFalg;
}
if (NumberOfAttachments > 1) {
formContext.ui.clearFormNotification("PackAttachID");
}
}
}
}
catch (error) {
console.log(error);
}
return nullCheckFalg;
}


return {
ValidationForPackagingStructure: validationForPackagingStructure,
ReturnValueFunction: returnValueFunction,
GlobalNullCheckFunction: globalNullCheckFunction
}

}());
}

Flag returns as undefined instead of boolean value true or false 



I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: The return value from a Asynchronous Xrm.WebApi is always undefined

    Hello,

    The place why your code doesn't work the way you want:

           var returnValueFunction = function(formContext) {

               var formContext = formContext;

               try {

                   validationForPackagingStructure(formContext).then(results => {//<<--- this is async callback in sync function

                       GlobalNullCheck = results;

                       return GlobalNullCheck;//<<--- this returns the data nowhere

                   });

               } catch {

                   console.log("Error");

               }

           };

    If you want to fix your code - either use only sync code or use async.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 60

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 16 Super User 2025 Season 2

#2
CU29080825-0 Profile Picture

CU29080825-0 16

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans