Skip to main content

Notifications

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
    a33ik 84,325 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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,382 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans