Sometimes we have to refresh CRM form using script. Till 2011 following script used to work fine:
window.location.reload(true);
But the following code was not working fine when we wanted to reload the page during save so we implemented following way:
A note here on successcallback and errorcallback methods can be found here:http://thedynamicscrmblog.wordpress.com/2013/12/04/crm-2013-client-api-save-refresh/
Xrm.Page.data.save().then(successCallback, errorCallback);
Xrm.Page.data.entity.save();
funtction successCallback()
{
//Needed to set form dirty to false explicitly as it is not done by platform
Xrm.Page.data.setFormDirty(false);
var Id = Xrm.Page.data.entity.getId();
Xrm.Utility.openEntityForm(“opportunity”, Id);
}
function errorCallback(attr1,attr2)
{
}
Hope it helps!
![]() |
Check this out |
Trying to Learn Dynamics CRM: Download!
Learn Dynamics CRM App on Google Play store
Hello Deepesh, can you please let me know where does the following code go ?
Xrm.Page.data.save().then(successCallback, errorCallback);
It goes to successCallback after form is saved in case everything is ok, else it goes to Errorcallback function. Add this wherever you need to refresh the form. Hope it helps!
Thanks for the reply. Perhaps I didn’t phrase my question properly – I mean to ask – where should the following line be placed in my JS webresource
Xrm.Page.data.save().then(successCallback, errorCallback);
I tried putting it OnLoad event function, but it just goes into infinite loop. I placed it in the OnSave event function, it gives me an error saying ‘Maximum call stack limit exceeded ‘ (for more than 30-40 times) . My form contains a lot of attributes and subgrids.
Can you please explain this to me as you would a beginner –
1. Where should i add the code to Append successCallback, errorCallBack functions,
2. What happens to my existing OnSave function (any changes required ? ) ?
It works! Really helpful!
Could you please better explain why there is “Xrm.Page.data.entity.save();” after the previous save?
Previous thing just appends to callback functions. Save happens only when you hit the above code. Hope it is more clear now!
Hi Deepesh, thanks for your response, but I still have three more questions:
1. Could you confirm that the first save doesn’t store the modified information (because it’s done by the second save)?
If so can we say that the first save is used only for shooting the instruction that made the page refresh?
2. Does Xrm.Page.data.entity.save() never update the isDirty property or this behaviour was due only to our procedure?
3. If we only use the first save, and leave the successCallback as is, will it save the new information although there is setFormDirty(false)?
Thanks for your attention
Answers below from what I experienced:
1.Yes, first one just appends callback in this case.
2. It does not (Its a noted issue I think on current scripts)
3. This did not work in 2013 when I tried.
Hope it helps!
Regards,
Deepesh
Now it’s clear! Thank you very much 😉
Hi sir, its vinod shivoor i guess you remember me.
thank you very much this helped me.
Thanks Vinod, do let me know if you need any help.
Hi,
I am trying to use this code in CRM 2015 and can’t get it to work.
Basically what is happening is that Xrm.Page.data.save().then(successCallback, errorCallback); actually saves the form as well (its not just registering the call back, it performs the save).
I never tried it in 2013 though. Has anything changed from 2013 to 2015?
Thanks,
Sina
What is the scenario in which you want a refresh?
In CRM 2015, on load of the form, I am filtering out (removing) some options set values from a specific picklist based on some business logic. When user saves the form, I need the page to reload so I get the options set original values.
Since in 2015, the page does not get reloaded, it is causing issues.
I know I can add the option set values back using the API, but since this is working perfect in 2011 and we just migrated, I was wondering if I can make it work.
Basically what I need is to register the callback function but without calling save from the javascript.
When I put this code on save or on load, it gets into a loop and eventually crashes because it tries to save. I am only using this line of code:
Xrm.Page.data.save().then(successCallback, errorCallback);
Thanks,
Sina
You do not need to call save but use this much bit
Xrm.Page.data.save().then(successCallback, errorCallback); wherever you want to bind the function on load
funtction successCallback()
{
//Needed to set form dirty to false explicitly as it is not done by platform
Xrm.Page.data.setFormDirty(false);
var Id = Xrm.Page.data.entity.getId();
Xrm.Utility.openEntityForm(“opportunity”, Id);
}
function errorCallback(attr1,attr2)
{
}
Yes, thats exactly what I am doing and what I explained above.
But the problem is Xrm.Page.data.save().then(successCallback, errorCallback);
actually tries to perform a save which causes issues.
Without a save, if you just refresh the page how will your form data retain?
Using Xrm.Page.data.refresh(false); is not bringing the optionset values back.
[…] Works!! …. until save. This is because after saving CRM refreshes some elements but doesn’t fire on my OnLoad function. You can find interesting way to refresh whole page after save: https://dynamicsofdynamicscrm.wordpress.com/2014/04/03/refresh-crm-2013-form-using-script/ […]
Hi Deepesh,
I tried following script on on save of the form but its going to infinite loop.
I want to save the modified form fields and refresh the form when user clicks on Save.
Please suggest if any thing wrong in my script
function autoRefresh()
{
if(Xrm.Page.ui.getFormType()==2)
{
Xrm.Page.data.save().then(successCallback, errorCallback);
Xrm.Page.data.entity.save();
function successCallback()
{
alert(“Success”);
//Needed to set form dirty to false explicitly as it is not done by platform
Xrm.Page.data.setFormDirty(false);
var Id = Xrm.Page.data.entity.getId();
Xrm.Utility.openEntityForm”incident”, Id);
}
function errorCallback(attr1,attr2)
{
alert(“Error”);
}
}
}
Hi Venkat,
When are you calling autorefresh?
Regards,
Deepesh
[…] Refresh CRM 2013 Form using script […]
[…] https://dynamicsofdynamicscrm.com/2014/04/03/refresh-crm-2013-form-using-script/ […]