Skip to main content

Notifications

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 



  • 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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,060 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,858 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans