Reputation: 39
Relatively new to JavaScript Promises.
Trying to call a function - checktheData(recordidGuid) from within a Promise and use the result in a .then chain.
The last line of the checktheData function is: return outcomeString
My problem is that the result of the promise is "undefined", yet when I output it to the console in the checktheData function it is as expected
I know I need to add a return statement, but where please?
Simple code:
function masterDeploy(PrimaryControl){
var formContext = PrimaryControl;
formContext.data.refresh(true);
var recordidGuid = ""
recordidGuid = formContext.data.entity.getId();
var loggedinuser =
Xrm.Utility.getGlobalContext().userSettings.userId;
recordidGuid = recordidGuid.replace('{', '').replace('}', '');
loggedinuser = loggedinuser.replace('{', '').replace('}', '');
var outcomeString = ""
var checkOutcome = false;
let promise = new Promise(function(resolve, reject) {
resolve(checktheData(recordidGuid))
});
promise.then(
result => {
console.log(promise);
}
),
promise.catch(
function (error){
DisplayError(error)}
);
return // stops here whilst debugging
if (checkOutcome == true){
var alertStrings = { confirmButtonLabel: "Close", text: outcomeString, title: "Data check failed" };
var alertOptions = { height: 500, width: 1000 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
function success(result) {
return;
},
function (error) {
DisplayError(error)
});
}
else{
if(checkOutcome == false){
//2. Create the learner journal url
buildLearnerEntryURL(recordidGuid,loggedinuser)
if(checkOutcome == false){
//3. Create refinery user and enrol on courses
createUserandEnrol(recordidGuid,loggedinuser)
if(checkOutcome == false){
//4. Create Learning Aims
createLearningAims(recordidGuid)
}
};
};
}
formContext.data.refresh(true);
//Set text to be a concatenated summary of actions completed or skipped
alertStrings = { confirmButtonLabel: "Close", text: "Deployment completed etc etc", title: "Deployment Completed" };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
function success(result) {
return;
},
function (error) {
DisplayError(error)
});
};
function checktheData(recordidGuid){
var outcomeString = ""
var checkOutcome = "false";
var str = "";
Xrm.WebApi.online.retrieveRecord("new_apprenticeshipflight", recordidGuid, "?$select=new_flighttype,new_programmelearningaimreference,new_apprenticeshipoverallaimreference,_new_mentor_value,_new_enrolledstandard_value,_new_clonelinkid_value,new_plannedstartdate,new_plannedenddate,crd80_overideendcalc,_new_contact_value,_new_apprenticeshipprogramme_value,_new_employer_value,_crd80_locationcontact_value,crd80_headofficedelivery,crd80_learningjournalurl,crd80_refineryid,crd80_addresstouse,_crd80_placementemployer_value,crd80_employeraddresstext,&$expand=new_Contact($select=emailaddress1,firstname,lastname),new_EnrolledStandard($select=crd80_learningjournalform),crd80_PlacementEmployer($select=name, accountid, address1_postalcode,address2_postalcode),crd80_LocationContact($select=emailaddress1,fullname,telephone1),new_Employer($select=name,address1_postalcode,address2_postalcode),new_Mentor($select=new_homepostcode),new_ApprenticeshipProgramme($select=crd80_learnerteamsiteurl)").then(
function success(result) {
if (result["new_flighttype"] == 100000001 && result["new_flighttype"] == null){outcomeString = outcomeString + "This must be a learner type flight"; checkOutcome = true};
//other lines removed for clarity
console.log(outcomeString)
return outcomeString
})
}
Upvotes: 1
Views: 407
Reputation: 5957
Xrm.WebApi.online.retrieveRecord
return a promise, so you can just return it from checktheData()
.
What you need to handle is basically just to call the checktheData()
and use .then()
and .catch()
for the result.
Example:
function masterDeploy(PrimaryControl) {
var formContext = PrimaryControl;
formContext.data.refresh(true);
var recordidGuid = "";
recordidGuid = formContext.data.entity.getId();
var loggedinuser = Xrm.Utility.getGlobalContext().userSettings.userId;
recordidGuid = recordidGuid.replace("{", "").replace("}", "");
loggedinuser = loggedinuser.replace("{", "").replace("}", "");
var outcomeString = "";
checktheData()
.then(checkOutcome => {
if (checkOutcome == true) {
var alertStrings = {
confirmButtonLabel: "Close",
text: outcomeString,
title: "Data check failed",
};
var alertOptions = { height: 500, width: 1000 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
function success(result) {
return;
},
function (error) {
DisplayError(error);
}
);
} else {
if (checkOutcome == false) {
//2. Create the learner journal url
buildLearnerEntryURL(recordidGuid, loggedinuser);
if (checkOutcome == false) {
//3. Create refinery user and enrol on courses
createUserandEnrol(recordidGuid, loggedinuser);
if (checkOutcome == false) {
//4. Create Learning Aims
createLearningAims(recordidGuid);
}
}
}
}
formContext.data.refresh(true);
//Set text to be a concatenated summary of actions completed or skipped
alertStrings = {
confirmButtonLabel: "Close",
text: "Deployment completed etc etc",
title: "Deployment Completed",
};
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions)
.then(function success(result) {
return;
})
.catch(error => {
DisplayError(error);
});
})
.catch(err => {
console.log(err);
});
}
function checktheData(recordidGuid) {
var outcomeString = "";
var checkOutcome = "false";
var str = "";
Xrm.WebApi.online
.retrieveRecord(
"new_apprenticeshipflight",
recordidGuid,
"?$select=new_flighttype,new_programmelearningaimreference,new_apprenticeshipoverallaimreference,_new_mentor_value,_new_enrolledstandard_value,_new_clonelinkid_value,new_plannedstartdate,new_plannedenddate,crd80_overideendcalc,_new_contact_value,_new_apprenticeshipprogramme_value,_new_employer_value,_crd80_locationcontact_value,crd80_headofficedelivery,crd80_learningjournalurl,crd80_refineryid,crd80_addresstouse,_crd80_placementemployer_value,crd80_employeraddresstext,&$expand=new_Contact($select=emailaddress1,firstname,lastname),new_EnrolledStandard($select=crd80_learningjournalform),crd80_PlacementEmployer($select=name, accountid, address1_postalcode,address2_postalcode),crd80_LocationContact($select=emailaddress1,fullname,telephone1),new_Employer($select=name,address1_postalcode,address2_postalcode),new_Mentor($select=new_homepostcode),new_ApprenticeshipProgramme($select=crd80_learnerteamsiteurl)"
)
.then(function success(result) {
if (
result["new_flighttype"] == 100000001 &&
result["new_flighttype"] == null
) {
outcomeString = outcomeString + "This must be a learner type flight";
checkOutcome = true;
}
//other lines removed for clarity
console.log(outcomeString);
return outcomeString;
});
}
Another example with async/await approach.
async function masterDeploy(PrimaryControl) {
var formContext = PrimaryControl;
formContext.data.refresh(true);
var recordidGuid = "";
recordidGuid = formContext.data.entity.getId();
var loggedinuser = Xrm.Utility.getGlobalContext().userSettings.userId;
recordidGuid = recordidGuid.replace("{", "").replace("}", "");
loggedinuser = loggedinuser.replace("{", "").replace("}", "");
var outcomeString = "";
const checkOutcome = await checktheData();
if (checkOutcome == true) {
var alertStrings = {
confirmButtonLabel: "Close",
text: outcomeString,
title: "Data check failed",
};
var alertOptions = { height: 500, width: 1000 };
try {
const result = await Xrm.Navigation.openAlertDialog(
alertStrings,
alertOptions
);
} catch (err) {
DisplayError(err);
}
} else {
if (checkOutcome == false) {
//2. Create the learner journal url
buildLearnerEntryURL(recordidGuid, loggedinuser);
if (checkOutcome == false) {
//3. Create refinery user and enrol on courses
createUserandEnrol(recordidGuid, loggedinuser);
if (checkOutcome == false) {
//4. Create Learning Aims
createLearningAims(recordidGuid);
}
}
}
}
formContext.data.refresh(true);
//Set text to be a concatenated summary of actions completed or skipped
alertStrings = {
confirmButtonLabel: "Close",
text: "Deployment completed etc etc",
title: "Deployment Completed",
};
try {
const result = await Xrm.Navigation.openAlertDialog(
alertStrings,
alertOptions
);
} catch (err) {
DisplayError(err);
}
}
async function checktheData(recordidGuid) {
var outcomeString = "";
var checkOutcome = "false";
var str = "";
const result = await Xrm.WebApi.online.retrieveRecord(
"new_apprenticeshipflight",
recordidGuid,
"?$select=new_flighttype,new_programmelearningaimreference,new_apprenticeshipoverallaimreference,_new_mentor_value,_new_enrolledstandard_value,_new_clonelinkid_value,new_plannedstartdate,new_plannedenddate,crd80_overideendcalc,_new_contact_value,_new_apprenticeshipprogramme_value,_new_employer_value,_crd80_locationcontact_value,crd80_headofficedelivery,crd80_learningjournalurl,crd80_refineryid,crd80_addresstouse,_crd80_placementemployer_value,crd80_employeraddresstext,&$expand=new_Contact($select=emailaddress1,firstname,lastname),new_EnrolledStandard($select=crd80_learningjournalform),crd80_PlacementEmployer($select=name, accountid, address1_postalcode,address2_postalcode),crd80_LocationContact($select=emailaddress1,fullname,telephone1),new_Employer($select=name,address1_postalcode,address2_postalcode),new_Mentor($select=new_homepostcode),new_ApprenticeshipProgramme($select=crd80_learnerteamsiteurl)"
);
if (
result["new_flighttype"] == 100000001 &&
result["new_flighttype"] == null
) {
outcomeString = outcomeString + "This must be a learner type flight";
checkOutcome = true;
}
//other lines removed for clarity
console.log(outcomeString);
return outcomeString;
}
Upvotes: 0