web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to prevent a save and refresh loop from external window?

(0) ShareShare
ReportReport
Posted on by

Hello everyone

Currently we have a plugin, that opens up a new window, allows users to enter data & then our connector passes this information back to CRM

Upon closing of the window, the code should refresh the record it was launched from. This is the original web resource script that has always worked since 2011

setInterval(function() { 
      if(name.closed) { 
         location.reload(); 
      } 
   }, 2000); 
if ( name == "null") { alert("Sheet did not open in a new window, probably due to popup blockers.");}


However in 2013 and 2015, if you run our plugin from a new record, when it refreshes the record, It creates a new one, rather than refreshing the saved opportunity. I believe I need to use the xrm.page.data.refresh() function instead.

The problem comes when I modify the code above to 

setInterval(function() { 
      if(name.closed) { 
         xrm.page.data.refresh(); 
      } 
   }, 2000); 
if ( name == "null") { alert("Sheet did not open in a new window, probably due to popup blockers.");}

When the window is closed, it does indeed refresh the record. The problem is, it refreshes it continuously and gets trapped in a loop. Due to the fact that the setInterval is set to occur every 2 seconds. 

I wish to run the function only once, but i've used setTimeout and I don't know whether it's because the window is closed, but the refresh of the crm record does not occur.

Any suggestions to get round this issue are welcome.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Daniel Wikell Profile Picture
    2,360 on at

    It sounds like you only want your method to run once. In that case you should use the clearInterval method to clear the timer right before the refresh statement

    You can read more about clear interval here: www.w3schools.com/.../met_win_clearinterval.asp

  • Alex Robinson Profile Picture
    on at

    Hi Daniel

    Does that mean I need to make the function a variable? Then add the clear interval to the script? If you could give me an example to work from that would be fantastic.

  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    on at

    Hi Alex,

    Refer this link:

    dynamicsofdynamicscrm.wordpress.com/.../refresh-crm-2013-form-using-script

    Hope this helps.

    Regards,

    R.Rajkumar

    "Please mark my answer as verified if you found it helpful"

  • Suggested answer
    Daniel Wikell Profile Picture
    2,360 on at

    Hi Alex

    setInterval returns an int which is the id of the scheduled function that is now running. If you provide the same number to the clearInterval method it will know which schedule to stop.

    You should be able to do it like so:

    var stopId = setInterval(function() { 
          if(name.closed) {
             clearInterval(stopId); 
             xrm.page.data.refresh(); 
          } 
    }, 2000); 
    

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans