locked
CRM 2011 - How to Refresh the Parent Form (JScript) RRS feed

  • Question

  • Hello Experts/Gurus,

    I have to refresh the parent form after the child form is closed, and have tried bunch of JScript snippets but nothing seems to be working.

    One of the MVP who has been great help at the board suggested me "in this case, there isn't a perfect solution for your requirements. You will probably have to educate your CRM users to refresh the Account form when they have made changes in Account Address form."

     

    Before i close this issue, i want to try one more time and post here to see if there is a solution to this:

    Here is my scenario:

    • I have Account and Account Address entities, for one account we can have multiple account addresses.
    • We call Account address from the left Navigation button on Account Form.
    • On the Account Address form based on requirement we are updating stuff on the Account form
    • After we close the Account address, we have to somehow refresh the calling form (i.e. Account) so users can see the changes. How do we accomplish this?

     

    Can you think of any work around. Please.

    Thanks, will look forward to hear from you.

    • Edited by itsgonabeme Thursday, August 11, 2011 3:12 AM formatting
    Thursday, August 11, 2011 3:10 AM

Answers

  • How about:
    window.parent.opener.parent.location.reload();
    

    As I understand it:
    • window is the iFrame holding the CRM form of the address entity
    • window.parent is the actual window for the address entity - the other suggestions did not include this
    • window.parent.opener is the iFrame for the associated view of the address entity from account
    • window.parent.opener.parent is the iFrame for the main part of the account form
    You could also try window.parent.opener.parent.parent.location.reload() - which is the entire window for the parent record.
    Obviously put this call in a try catch or test first that the objects you need are defined in case someone opens the account address form from anywhere other than the parent account form.
    • Marked as answer by itsgonabeme Thursday, August 18, 2011 8:31 PM
    Thursday, August 11, 2011 4:25 PM
  • Did you try to window.opener.parent.location.reload ??
    Mahain : MS CRM Freelancer
    • Proposed as answer by DotNetMonkey Friday, August 12, 2011 3:35 PM
    • Marked as answer by itsgonabeme Thursday, August 18, 2011 8:31 PM
    Thursday, August 11, 2011 5:06 AM
    Moderator
  • I have just tested the following function in the on load event of the appointment form and it works for me. I create the appointment from a contact so this ID is filled in the regarding field. The code uses the REST endpoint to update the parent contact and then refreshes the parent form.

    function RefreshParent() {
      var oContext = Xrm.Page.context;
      var sUrl = oContext.getServerUrl();
      sUrl += "/XRMServices/2011/OrganizationData.svc";
      var oUpdate = new XMLHttpRequest();
      var sId = Xrm.Page.getAttribute("regardingobjectid").getValue()[0].id.toString().substring(1, 37);
      alert(sId);
      sUrl += "/ContactSet(guid'" + sId + "')";
      alert(sUrl);
      oUpdate.open("POST", sUrl, false);
      oUpdate.setRequestHeader("Accept", "application/json");
      oUpdate.setRequestHeader("Content-Type", "application/json; charset=utf-8");
      oUpdate.setRequestHeader("X-HTTP-Method", "MERGE");
    
      oUpdate.send("{ \"FirstName\" : \"" + new Date().toString() + "\"}"); // update contact first name to current date and time
      alert(oUpdate.status);
    
      window.parent.opener.parent.location.reload(); // refresh contact form
     }
    
    

    If you are triggering in the on load event, when are you making the changes? The parent form might be refreshing before any changes have been made. If the changes are made as users interact with the child form then you should do the update in onSave.

    • Marked as answer by itsgonabeme Thursday, August 18, 2011 8:31 PM
    Friday, August 12, 2011 9:23 AM

All replies

  • Did you try to window.opener.parent.location.reload ??
    Mahain : MS CRM Freelancer
    • Proposed as answer by DotNetMonkey Friday, August 12, 2011 3:35 PM
    • Marked as answer by itsgonabeme Thursday, August 18, 2011 8:31 PM
    Thursday, August 11, 2011 5:06 AM
    Moderator
  • No i haven't, let me try and i will get back to you.
    Thursday, August 11, 2011 8:45 AM
  • No it doesn't work.

    I have binded the script with the OnLoad and OnSave of Account Address (child form) and in both instances i get following errors:

     

    ---------------------------
    Message from webpage
    ---------------------------
    There was an error with this field's customized event.

    Field:crmForm

    Event:onsave

    Error:Unable to get value of the property 'parent': object is null or undefined
    ---------------------------
    OK  
    ---------------------------
       

    ---------------------------
    Message from webpage
    ---------------------------
    There was an error with this field's customized event.

    Field:window

    Event:onload

    Error:Unable to get value of the property 'parent': object is null or undefined
    ---------------------------
    OK  
    ------------

     

    Thursday, August 11, 2011 8:53 AM
  • You can also try windows.opener.location.reload().

    Michael

    Thursday, August 11, 2011 4:02 PM
  • How about:
    window.parent.opener.parent.location.reload();
    

    As I understand it:
    • window is the iFrame holding the CRM form of the address entity
    • window.parent is the actual window for the address entity - the other suggestions did not include this
    • window.parent.opener is the iFrame for the associated view of the address entity from account
    • window.parent.opener.parent is the iFrame for the main part of the account form
    You could also try window.parent.opener.parent.parent.location.reload() - which is the entire window for the parent record.
    Obviously put this call in a try catch or test first that the objects you need are defined in case someone opens the account address form from anywhere other than the parent account form.
    • Marked as answer by itsgonabeme Thursday, August 18, 2011 8:31 PM
    Thursday, August 11, 2011 4:25 PM
  • @Michael: Thanks for the suggestion but it didn't work.

    @Luku, Both of the snippets didn't work, Thank you so much for helping me understand the concept on how the Jscript works

     

    So the concept of this flow is same as what we have in base CRM (Contact --> Activities):

    • We open Contact Form
    • From Contact Form on the left navigation we click Activities (the contact/activities associated list view is shown in the same window)
    • Then we double click the existing activity or we create a new Activity which opens up the new window
    • Again the question is in that new window of new/edit activity i am modify some stuff on the main Contact card and as soon as user closes the form i want that parent window to refresh.
    • The only triggers we have is onLoad and onSave, and currently i am binding all the code snippet onLoad of Account address or Activites form

     

    Hope i have explained in correctly.

    Thursday, August 11, 2011 6:32 PM
  • I have just tested the following function in the on load event of the appointment form and it works for me. I create the appointment from a contact so this ID is filled in the regarding field. The code uses the REST endpoint to update the parent contact and then refreshes the parent form.

    function RefreshParent() {
      var oContext = Xrm.Page.context;
      var sUrl = oContext.getServerUrl();
      sUrl += "/XRMServices/2011/OrganizationData.svc";
      var oUpdate = new XMLHttpRequest();
      var sId = Xrm.Page.getAttribute("regardingobjectid").getValue()[0].id.toString().substring(1, 37);
      alert(sId);
      sUrl += "/ContactSet(guid'" + sId + "')";
      alert(sUrl);
      oUpdate.open("POST", sUrl, false);
      oUpdate.setRequestHeader("Accept", "application/json");
      oUpdate.setRequestHeader("Content-Type", "application/json; charset=utf-8");
      oUpdate.setRequestHeader("X-HTTP-Method", "MERGE");
    
      oUpdate.send("{ \"FirstName\" : \"" + new Date().toString() + "\"}"); // update contact first name to current date and time
      alert(oUpdate.status);
    
      window.parent.opener.parent.location.reload(); // refresh contact form
     }
    
    

    If you are triggering in the on load event, when are you making the changes? The parent form might be refreshing before any changes have been made. If the changes are made as users interact with the child form then you should do the update in onSave.

    • Marked as answer by itsgonabeme Thursday, August 18, 2011 8:31 PM
    Friday, August 12, 2011 9:23 AM
  • So basically in the above function if i take the REST code off and keep the script

    window.parent.opener.parent.location.reload(); 

    then we are talking about the same thing that we have tried and we get the same error.

    I tried both the OnLoad and OnSave and nothing worked.

     

    Friday, August 12, 2011 2:59 PM
  • What is there error you are getting? Is is the same one you posted before?

    The script that I posted above worked for me in CRM 2011 so I don't see why it's not working for you.

    Tuesday, August 16, 2011 1:36 PM
  • Hey Luke, i have tested your stuff on another solution and its working all good.

    It looks like the root cause is plugins, with plugins code it will not work.

    But thank for the helping me understand this + also i will keep this code snippet handy.

    Kind Regards

    Thursday, August 18, 2011 8:31 PM
  • Hi Luke,

    Above code i am using to update account field from child entity onsave event,

    but its gives me error: access denied.

    I had added all the sites to trusted Zone, still its says access denied,

     

    Please help-Suggest.

    Thanks,

    Govind Bhanushali

    Friday, September 9, 2011 7:57 AM
  • Hi Govind,

    Did you try to debug your code and check on which line your are getting this error ??


    Mahain : MS CRM Freelancer
    Friday, September 9, 2011 8:28 AM
    Moderator
  • Hello,

    I am also trying to do refresh the parent form when the child record is getting created. However, I am getting access denied error when I am executing this line " window.opener.parent.location.reload()".

    Fyi, I have an iframe in my parent form and I want to refresh this iframe whenever the child record is created.

    Please advise me!. Thanks!


    Vineet
    Tuesday, January 3, 2012 6:22 AM
  • Hello,

    My subgrid is update by the plugin code.

    In fact i have two entities called action and client.

    There is a subgrid of action in client, and i need this subgrid reload once

    number of action is updated ( creating or deleting of action).

    How can i do it?

    Please  help.

    Friday, January 6, 2012 2:44 PM
  • Yes i'm trying all code but i got the same error at line

     window.parent.opener.parent.location.reload(); // refresh contact form

     

    Acces denied, what doesn't mean.

     

    More over i need to refresh ribbon if action is deleted because i change color of activities depends on the number.

     

    Friday, January 6, 2012 3:30 PM