In this blog, we will show how to prevent or restrict saving based on field values in dynamics 365

For this blog I have created below fields.  if Status Reason is Active and below any of the field is empty(father,mail,coun) the code will stop saving the form.

Additionally I am giving an alert when the form prevents saving

Code

 

function preventsave(executionContext){
var formContext= executionContext.getFormContext();
var status= formContext.getAttribute(“statuscode”).getValue();
var fathername=formContext.getAttribute(“vinay_father”).getValue();
var mail=formContext.getAttribute(“vinay_mail”).getValue();
var count=formContext.getAttribute(“vinay_coun”).getValue();
var eventArgs=executionContext.getEventArgs();

if(status == 1){
if(fathername == null || mail == null || count == null){
eventArgs.preventDefault();
let alertstings={
ConfirmButtonLabe: “ok”,
text:”This is just a test of vinay to stop saving the record”,
title:”Alert”
};
let alertoptions={
height: 120,
width: 260
};
Xrm.Navigation.openAlertDialog(alertstings, alertoptions).then(
function(success){
console.log(“Alert Dialog Closed”);
},
function(error){
console.log(error.message);
}
);
}
}
}

Now we can see its working with the above code and if any field is empty when status reason is Active and the form will be unsaved

For any Help or Queries contact us on info@crmonce.com or +918096556344