Dynamics 365 – Running JavaScript on Activate and Deactivate

Leave a comment

In Dynamics 365, some entities contain the ability to activate and deactivate records. For example, with Accounts:

Clicking Deactivate will raise an alert that will prompt the user to Confirm Deactivation:

Which then changes the status of the item to Inactive:

We can add JavaScript code to the OnSave event of this entity that will tell us when a record status has changed to activated or deactivated. For example:

Edit the form and

Click Add:

And New to add a new Web Resource:

Add a new JScript resource:

We can use getSaveMode to determine the event value, where:

  • Deactivate = 5
  • Activate = 6

Add the code:

function accountOnSave(econtext) {
    var eventArgs = econtext.getEventArgs();
    if (eventArgs.getSaveMode() == 5) {
        alert("Deactivated");
    }
    if (eventArgs.getSaveMode() == 6) {
        alert("Activated");
    }
}

Publish the code.

Now, change the form to OnSave and click Add to add our library and function:

Select the library, function and make sure to select to Pass execution context as first parameter:

Click OK and save and publish the form. Refresh the form.

Now, click Deactivate to see the alert:

And Activate:

You can also use preventDefault() to stop the activate or deactivate from occurring:

eventArgs.preventDefault();

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *