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

Announcements

No record found.

News and Announcements icon
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

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
NeerajPawar Profile Picture

NeerajPawar 31

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 19 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 18

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans