Dynamics 365 – Xrm.Page.data.refresh and Xrm.Page.data.save

1 Comment

Dynamics 365 has a couple of JavaScript functions that are useful in refreshing and saving pages – xrm.page.data.refresh and xrm.page.data.save. We will go though examples of using these.

The refresh function is defined as:

Xrm.Page.data.refresh(save).then(successCallback, errorCallback);

We can pass an optional save boolean as well.

For the purpose of the demo, let’s say when tabbing off the Account’s website field, we want to refresh the current page.

Our code:

function RunJavaScript() {
 Xrm.Page.data.refresh();
}

Now, if we change change the Fax field, and then change the Website field to run the script, we can see the page is trying to refresh, and changes have not been saved:

If we click OK, the page refreshes and we lose our changes:

Now if we change our code to add save:

 Xrm.Page.data.refresh(true);

And update the fax and website field, on tabbing off the website you will see in the bottom right “unsaved changes” changes to “saving”:

Now let’s change this to use the save() function. We will display an alert on success or failure:

 Xrm.Page.data.save().then(function() { alert("Saved!"); }, function() { alert("Failed!"); });

Change the website, you will see the page save, then display an alert:

If we try to save without entering the required fields, we get the message:

 

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

 

One Response to Dynamics 365 – Xrm.Page.data.refresh and Xrm.Page.data.save

  1. This was awesome, saved me a ton of time, how do I call a save from within an HTML page on a button click? I have an html button and I’m updating fields and I want to save and refresh.

Leave a Reply

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