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
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,902 Super User 2024 Season 2
Martin Dráb 229,316 Most Valuable Professional
nmaenpaa 101,156