Advertisements

Archive

Posts Tagged ‘Prevent Save’

D 365 – Prevent Record’s Deactivate/Activate from Form – JScript

Lets see how we can apply our validation logic and prevent Deactivate/Activate of a record from Form.

Its a 3 step process:

  • Register script on form’s ‘OnSave’ event
  • Capture the ‘Deactivate/Activate’ event (i.e., by reading ‘SaveMode’ from Context)
  • Prevent Save operation

Below is the snippet to validate and prevent the Save by reading the type of operation (i.e., Deactivate, Activate, Auto Save, Save and Close, Save and New etc…).

Script :

function onSave(econtext) {
var eventArgs = econtext.getEventArgs();

// Capture Deactivation(i.e., SaveMode=5) or Activation(i.e., SaveMode=6)
if (eventArgs.getSaveMode() == 5 || eventArgs.getSaveMode() == 6) {
// Your business logic goes here
Xrm.Utility.alertDialog(“Preventing Deactivate or Activate”);
eventArgs.preventDefault();
}

// Capture AutoSave event (i.e., SaveMode=70)
if (eventArgs.getSaveMode() == 70) {
Xrm.Utility.alertDialog(“Preventing Auto Save”);
eventArgs.preventDefault();
}

// Capture ‘Save and Close’ event (i.e., SaveMode=2)
if (eventArgs.getSaveMode() == 2) {
Xrm.Utility.alertDialog(“Preventing ‘Save and Close'”);
eventArgs.preventDefault();
}
}

How to use the script:

  • Register above script on form’s ‘onsave’ event.
  • Make sure you selected ‘Pass execution context as first parameter’ checkbox.

Save_3

  • Save and Publish
  • Open the record and try to ‘Deactivate’ and you should get below popup.

Save_2

Note:

  • Script triggers only when you Deactivate\Activate only from Form.
  • It does not work from Grids or ‘Advanced find’

Refer article for the complete list of ‘SaveMode’ values.

🙂

Advertisements
Categories: CRM Tags: ,