Dynamics 365 addOnSave JavaScript method

Leave a comment

The addOnSave method is used to add a function to the OnSave event. Let’s go through an example of how this is used.

First we will define a new JavaScript function on the Account form. Open the Account form in Customizations and click to add a new Form Library:

Select New:

We will call this TestFunctions. Click Next:

Create a new function. We will create a function to display the Organization name:

function displayOrgName(execContext)
{
    try {
        alert(execContext.getContext().getOrgUniqueName());
    }
    catch (e) {
        alert(e.message);
    }
}

Now, we will add code to call the addOnSave method:

function addMessageToOnSave() {
    Xrm.Page.data.entity.addOnSave(displayOrgName);
}

Click OK and Publish:

Add the new JScript to the form properties. Note we are not adding anything to the OnSave event:

Now add the new function to the OnLoad event. When the form loads, the function will be called to register our handler:

Save and publish the form.

Now, go into an account record, make a change and save. You will get an alert showing the organization name.

Note per Microsoft’s documentation: The function will be added to the bottom of the event handler pipeline. The execution context is automatically set to be passed as the first parameter passed to event handlers set using this method.

Note there is also a removeOnSave function.

THANKS FOR READING. BEFORE YOU LEAVE, 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

 

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

 

Leave a Reply

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