Hi everyone,
I have an event on save which consists of an API call to check some status. Based on that status i am trying to save changes. So, for this, i used preventDefault to cancel the save operation which I did before the API call.
When i try to save the form again with "FormContext.data.save()", it throws "This form can’t be saved due to a custom setting". I also tried using preventDefault after the api call but the form was saved before hitting the response of the api.
Please find below my code:
// JavaScript source code
function onAccountFormLoad(executionContext) {
debugger;
var formContext = executionContext.getFormContext();
formContext.data.entity.addOnSave(
(() => {
return async (eContext) => {
eContext.getEventArgs().preventDefaultOnError();
let result = await validateSaveAsync(eContext);
}
}
)());
}
var validateSaveAsync = (eContext) => {
var formContext = eContext.getFormContext();
var agreement = formContext.getAttribute("msdyn_agreement").getValue();
return new Promise(function(resolve, reject) {
var boolRule1 = true;
if (agreement != null && agreement.length > 0) {
var id = agreement[0].id;
eContext.getEventArgs().preventDefault();
var result = Xrm.WebApi.retrieveRecord("msdyn_agreement", id, "?$select=msdyn_systemstatus").then(
function success(result) {
if (result["msdyn_systemstatus"] === 690970003) {
//Xrm.Navigation.openAlertDialog({text:"Current work order can't be saved because of Cancelled agreement"});
boolRule1 = false;
}else{
var formContextTest = eContext.getFormContext();
formContextTest.data.save().then(function() {
alert("Form saved successfully");
}, function(error) {
var errorMessage = error.message;
console.log(errorMessage);
});
}
resolve(boolRule1);
},
function(error) {
//Xrm.Navigation.openAlertDialog({ text: error.message });
reject(error.message);
}
);
} else {
resolve(boolRule1);
}
})
}
Please find below the screenshot error message:
What I am missing or doing wrong? Can please someone provide an example to solve the above scenario?
Any advice is highly appreciated.
Thank you!