locked
attachevent on SubGrid onrefresh event

    Question

  • I wrote some some JavaScript code in CRM 4.0 that triggers when the datagrid inside of an IFrame refreshes.  I have upgraded the system to CRM 2011 and changed the IFrame to a SubGrid.  Now I am trying to use JavaScript to attach the function to the onrefresh event and can't seem to get it to work.

    This is the old code I need to reproduce.

    if (crmForm.FormType != 1) {

        crmForm.all.IFRAME_OpportunityLines.src = GetFrameSource("fj_opportunity_new_opportunitymanagementmodu");
        crmForm.all.IFRAME_OpportunityLines.onreadystatechange = function oppLineTotals() {
            if (crmForm.all.IFRAME_OpportunityLines.readyState == 'complete') {
                var iFrame = frames[window.event.srcElement.id];
                iFrame.document.all.crmGrid.attachEvent("onrefresh", GridRefresh);
            }
        }
    }

    Any help would be appreciated.

    Thanks
    Saturday, March 5, 2011 1:27 AM

Answers

  • FYI - Anyone who is trying to do this after RU12, the following code will work:

    var SubGrid = document.getelementById(GridName);

    //SubGrid.attachEvent("onrefresh", CallbackFunction);

    SubGrid.control.add_onRefresh(CallbackFunction);

    Thanks to Alberto for pointing me in the right direction :)

    Wednesday, February 13, 2013 7:19 PM
  • Hi Yoann,

    yes you have right. All customizations that use the grid.htc don't work anymore.

    So the code attachEvent("onrefresh"....) is not valid and is not cross-browser compatible.

    I don't know if there is a certified way to obtain the result....

    However I think that (although it is not certified by SDK) if you ovveride the function add_onRefresh  of you sub-grid you are able to obtain the same result.

    I hope this help you,

    Alberto


    Alberto




    • Edited by Alberto_26 Saturday, February 2, 2013 7:23 PM
    • Marked as answer by HIMBAPModerator Monday, February 25, 2013 5:48 PM
    Saturday, February 2, 2013 7:11 PM

All replies

  • Hello,

    For CRM 2011, your code should look like this:

    if (Xrm.Page.ui.getFormType() != 1) {
    
      Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").setSrc(GetFrameSource("fj_opportunity_new_opportunitymanagementmodu"));;
      Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").onreadystatechange = function oppLineTotals() {
        if (Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").readyState == 'complete') {
          var iFrame = frames[window.event.srcElement.id];
          iFrame.document.all.crmGrid.attachEvent("onrefresh", GridRefresh);
        }
      }
    }
    
    
    Also, you can use this JavaScript converter tool: http://bingsoft.wordpress.com/2010/09/09/crm-4-to-crm-2011-javascript-converter-tool/.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Saturday, March 5, 2011 6:22 AM
  • Cornel,

    Since I converted the IFrame to a SubGrid this will not work.  The content of the IFrame was a N:1 Relationship Entity.  I guess I could try reverting it back to an IFRAME in 2011 but I would prefer to use the SubGrid and figure out how to accomplish the same thing.

    Thanks for your response.

    Saturday, March 5, 2011 3:54 PM
  • Hy Cornel

    I'm also interested in trigger some javascript on the parent, when something in the sub-grid changes. Have you found a solution?

    Thanks,

    Peter

    • Proposed as answer by EleanaP Wednesday, August 7, 2013 7:26 AM
    • Unproposed as answer by EleanaP Wednesday, August 7, 2013 7:26 AM
    Tuesday, March 29, 2011 6:26 AM
  • I've also just encountered the need for this.  Any help appreciated.

    Thanks,

    xRM2011User

    Monday, April 4, 2011 2:13 PM
  • having same problem any solution ?

    Monday, April 25, 2011 3:57 AM
  • Funny thing, the sdk recomended function did not work for me (i.e. Xrm.Page.ui.controls.get("GridName") ), and yet the following works (tested!)

     

    var grid = document.getElementById("GridName");
    grid.attachEvent("onrefresh", EventHandlerFunction);
    

     

    because the grid.htc (Html Component) actually includes a public event "onrefresh"... And this event can be handled with any function. We dont need to dive into the eventManager and scriptEvents.

    • Edited by Can Bilgin Monday, April 25, 2011 9:44 AM Additional Info
    • Proposed as answer by Aswathy S Wednesday, April 27, 2011 3:54 AM
    Monday, April 25, 2011 9:40 AM
  • Hi,

    When I use Xrm.Page.ui.controls.get("GridName") , get "Unable to get value of the property 'controls': object is null or undefined" error so I tried document.getElementById("GridName") and got "Unable to get value of the property 'attachEvent': object is null or undefined".

    Do you have any idea?

    Thanks,

    Sunday, May 29, 2011 9:27 AM
  • I am not sure what the problem is... see http://www.quirksmode.org/js/events_advanced.html (the microsoft model - attachEvent). This might be that the document.getElementById("Grid") returns null.
    Wednesday, June 1, 2011 5:09 PM
  • Hello,

    To refresh the contents of the grid when a new item is added you can use the following code 

    function
     form_onload() {
        var grid = document.getElementById("PR_Line_Items");
        grid.attachEvent("onrefresh", ReloadPurchaseRequest);
    }
     
    function ReloadPurchaseRequest() {
        window.location.reload(true);
    }
    Friday, January 18, 2013 6:52 PM
  • Hi, It's seem that after Polaris Update this js code doesn't work anymore.

    Someone can help me ?

    Thursday, January 31, 2013 2:57 PM
  • Hi Yoann,

    yes you have right. All customizations that use the grid.htc don't work anymore.

    So the code attachEvent("onrefresh"....) is not valid and is not cross-browser compatible.

    I don't know if there is a certified way to obtain the result....

    However I think that (although it is not certified by SDK) if you ovveride the function add_onRefresh  of you sub-grid you are able to obtain the same result.

    I hope this help you,

    Alberto


    Alberto




    • Edited by Alberto_26 Saturday, February 2, 2013 7:23 PM
    • Marked as answer by HIMBAPModerator Monday, February 25, 2013 5:48 PM
    Saturday, February 2, 2013 7:11 PM
  • FYI - Anyone who is trying to do this after RU12, the following code will work:

    var SubGrid = document.getelementById(GridName);

    //SubGrid.attachEvent("onrefresh", CallbackFunction);

    SubGrid.control.add_onRefresh(CallbackFunction);

    Thanks to Alberto for pointing me in the right direction :)

    Wednesday, February 13, 2013 7:19 PM
  • Hi David,

    Thanks.

    If you want to want to write in standard jquery you can use this code:

    $find("gridId").add_onRefresh = function NewRefresh(value) {

    //standard Microsoft

    this.get_events().addHandler("OnRefresh",value);

    //your update must following

    }

    Hope you understand my solution,

    Alberto


    Alberto

    Thursday, February 14, 2013 7:51 AM