I have a Model Driven app with business process flow that I am trying to prevent the users from progressing if there is not records in a subgrid. I can get a dialog to pop, but the bpf advances to the next stage even when preventDefault is invoked.
formContext.data.process.addOnPreStageChange (Sdk.validateApprovers);
this.validateApprovers = async function (executionContext) {
let formContext = executionContext.getFormContext();
let activeStage = formContext.data.process.getActiveStage().getName();
let terminationId = formContext.data.entity.getId().replace(/[{}]/g, '');
formContext.ui.clearFormNotification("ApproverError");
formContext.ui.clearFormNotification("RecipientError");
if (activeStage = 'Configuration') {
let approverOptions = `?$select=new_approverid&$filter=statecode eq 0 and _new_relatedrecordid_value eq ${terminationId}`;
let recipientOptions = `?$select=new_recipientid&$filter=statecode eq 0 and _new_relatedrecord_value eq ${terminationId}`;
//isApproverRows = false;
//isRecipientRows = false;
await Xrm.WebApi.retrieveMultipleRecords("new_recipient", recipientOptions).then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
console.log(result.entities[i]);
}
if (result.entities.length === 0) {
let alertStrings = {
confirmButtonLabel: "OK",
text: "Recipients are not defined. Please specify.",
title: "Warning"
};
executionContext.getEventArgs().preventDefault();
Xrm.Navigation.openAlertDialog(alertStrings);
}
},
function (error) {
console.log(error.message);
}
);
}
}
I believe the reason is that you have an async call and preventDefault happens in the callback when the save happened already. I believe you will have to use an approach similar to the one I used before but for a pure save event - https://butenko.pro/2018/11/15/cancelling-save-based-on-the-result-of-async-operation/ Personally, I have never worked with addOnPreStageChange handler so I have no idea if would it work or not.
In the worst case scenario you can create a real-time workflow/plugin that will do checks and raise an error to prevent the save.