Skip to main content

Refresh CRM 2016 Form after record save using JavaScript


    I had a need to refresh crm 2016 form right after initial save. Surprisingly all good old solution from my archive were not effective in crm 2016. Neither window.location.reload(), nor sdk’s refresh, nor trivial setTimeout. "setTimeout" would work if not bunch of heavy plugins and JavaScrippt validation on save event.

    I found that Google knows about it as many as I do. Okay, no copy past this time J


    After half an hour I found a nice solution with really minimal risk of browser leaking. The idea based on fact that OnLoad event occurs without form refresh right after change of form type. We need an independent flag to catch that. The code below does the trick.

 function refreshFormAfterInitialSave() {  
     if (Xrm.Page.ui.getFormType() == 1) {  
       top.callN = 1;  
     }  
     if (Xrm.Page.ui.getFormType() == 2 && top.callN == 1) {  
        setTimeout(function () {           
         top.callN = null;
         Xrm.Utility.openEntityForm(Xrm.Page.data.entity.getEntityName(), Xrm.Page.data.entity.getId()); 
       }, 2000);
     }  
 //////////////////////////////////////////  
 OnLoad: function (context) {  
       //set id for first call after save  
       refreshFormAfterInitialSave();  
     },  


    It works fine and refreshes the form only once after initial save. If you hit new on existing form, it will work correctly too for new form an its heirs. I’m not sure I’ve made all possible tests but hope it saved your time.

Happy coding!

Comments

Sushma said…
It does not work. Onload event does not fire on change of form mode
Tobias said…
I've added a working solution to my blog.

https://tobiaskoller.blogspot.de/2018/02/dynamics-crm-365-and-earlier-after-save.html
vandana said…
It worked for me, I just change one command and increased timeout time

function refreshFormAfterInitialSave() {
if (Xrm.Page.ui.getFormType() == 1) {
top.callN = 1;
}
if (Xrm.Page.ui.getFormType() == 2 && top.callN == 1) {
setTimeout(function () {
top.callN = null;
Xrm.Page.data.refresh();
}, 3500);
}
}
hema said…
Excellent idea. Thank you for sharing the useful information. Share more updates.
QlikView Online Course
Azure Online Training
Pega Online Course

Most popular

Dynamics 365 online and Adx portal on-premise. Are they compatible?

Adxstudio Portal how to: Incremental (Sequential) Deployment and backup. Part I

Custom plugin exception output for crm form