Dynamics 365 – Running JavaScript on Activate and Deactivate

1 Comment
BEFORE WE START, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

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, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Dynamics 365 – Running JavaScript on Activate and Deactivate

  1. Carl,
    I was trying this but I am getting as 1 for recativate case and resolve case. I am using Dynamics 365 V9. Do we have different syntax?

    Thank You
    Rupesh

Leave a Reply

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