Refresh CRM 2013 Form using script

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

22 thoughts on “Refresh CRM 2013 Form using script

  1. Hello Deepesh, can you please let me know where does the following code go ?

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

      • 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 ? ) ?

      • 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

  2. 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

  3. 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

  4. 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)

    {

    }

  5. 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.

  6. 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”);
    }

    }
    }

Leave a comment