locked
CRM 4 - Hooking the CRMGrid to trigger user defined "onclick" javascript function RRS feed

  • Question

  • I would like to hook & play with CRMGrid onclick, ondblclick and etc... Any sample codes/references?

    Tried attaching "custom onclick" event with the function AttachEvent but, even after setting the cancenBubble property of window & crmGrid i am getting CRM error message...
    Monday, March 31, 2008 8:13 AM

Answers

  •  

    Hi.

     

    The code below binds to the crmGrid inside an IFRAME.

    Change the iframe name to yours.

     

    var IFRAME_TEST = 'IFRAME_TEST';

     

    function OnCrmPageLoad()

    {

       //onclick event

       BindToCrmGridRow();

    }

     

    function GetIFrame( iframeId ){

          return document.getElementById(iframeId);

    }

     

    function GetCrmGrid( iframeId ){

          return GetIFrame( iframeId ).document.frames[iframeId].document.all.crmGrid

    }

     

    function BindToCrmGridRow()

    {

        var contactIframe = GetIFrame(IFRAME_TEST);

                contactIframe.attachEvent( "onreadystatechange" , OnIframeReady );

    }

     

    function OnIframeReady()

    {

          var contactIframe = GetIFrame(IFRAME_TEST);

          if( contactIframe.readyState != 'complete' )

                return;

               

          var crmGrid = GetCrmGrid(IFRAME_TEST);

                crmGrid.attachEvent( "onclick" , OnCrmGridClick );

    }

     

    function OnCrmGridClick()

    {

          var crmGrid = GetCrmGrid(IFRAME_TEST); 

          // SelectedRecords[0] contains all the information you need and more...

          alert( crmGrid.InnerGrid.SelectedRecords[0][0] );

    }

     

    Cheers,

    Adi

    Friday, April 4, 2008 2:10 AM

All replies

  • Surprisingly no volunteer(s)!!!!!!!! Wondering why???

    Ok any how... Will explain my scenario.... Here it goes

    In some CRM Grids I would like to add few icons and when the users click the icon relavent screen have to be opened... I've tried this and I can achive this in few form but not all.... Most of the forms throws CRM error something like below.

    CRM Error Report

    Microsoft Dynamics CRM Error Report Contents

    <CrmScriptErrorReport>
     <ReportVersion>1.0</ReportVersion>
     <ScriptErrorDetails>
     <Message>Object expected</Message>
     <Line>1</Line>
     <URL>/AdvancedFind&#47;fetchData.aspx&#63;EntityCode&#61;4210&#38;QueryId&#61;&#123;F6A2F891-6846-4B21-B922-712695AFD65C&#125;&#38;ViewType&#61;1039</URL>
     <PageURL>/AdvancedFind&#47;fetchData.aspx&#63;EntityCode&#61;4210&#38;QueryId&#61;&#123;F6A2F891-6846-4B21-B922-712695AFD65C&#125;&#38;ViewType&#61;1039</PageURL>
     <Function>function anonymous&#40;&#41;&#10;&#123;&#10;return loadContacts&#40;1&#41;&#59; event.cancelBubble&#61;true&#59;&#10;&#125;</Function>
     <CallStack>
     <Function>function anonymous&#40;&#41;&#10;&#123;&#10;return loadContacts&#40;1&#41;&#59; event.cancelBubble&#61;true&#59;&#10;&#125;</Function>
     </CallStack>
     </ScriptErrorDetails>
     <ClientInformation>
     <BrowserUserAgent>Mozilla&#47;4.0 &#40;compatible&#59; MSIE 7.0&#59; Windows NT 6.0&#59; SLCC1&#59; .NET CLR 2.0.50727&#59; Media Center PC 5.0&#59; .NET CLR 3.0.04506&#59; .NET CLR 3.5.21022&#59; MS-RTC LM 8&#41;</BrowserUserAgent>
     <BrowserLanguage>en-us</BrowserLanguage>
     <SystemLanguage>en-gb</SystemLanguage>
     <UserLanguage>en-gb</UserLanguage>
     <ScreenResolution>1440x900</ScreenResolution>
     <ClientName>Web</ClientName>
     </ClientInformation>
     <ServerInformation>
     <OrgLanguage>1033</OrgLanguage>
     <OrgCulture>2057</OrgCulture>
     <UserLanguage>1033</UserLanguage>
     <UserCulture>2057</UserCulture>
     <OrgID>&#123;8E5EB426-352A-402D-A3BA-85BE88096C04&#125;</OrgID>
     <UserID>&#123;CB375E51-65F2-DC11-9260-0003FF69D9A1&#125;</UserID>
     <CRMVersion>4.0.7333.3</CRMVersion>
     </ServerInformation>
    </CrmScriptErrorReport>

    <CrmScriptErrorReport>
     <ReportVersion>1.0</ReportVersion>
     <ScriptErrorDetails>
     <Message>&#39;cells.0&#39; is null or not an object</Message>
     <Line>994</Line>
     <URL>/_static&#47;_grid&#47;appgrid_defaultdata.htc</URL>
     <PageURL>/AdvancedFind&#47;fetchData.aspx&#63;EntityCode&#61;4210&#38;QueryId&#61;&#123;F6A2F891-6846-4B21-B922-712695AFD65C&#125;&#38;ViewType&#61;1039</PageURL>
     <Function>applyStyle&#40;o, sClass&#41;</Function>
     <CallStack>
     <Function>applyStyle&#40;o, sClass&#41;</Function>
     <Function>selectRow&#40;oRow, fRaiseEvent&#41;</Function>
     <Function>UnselectRecords&#40;oSkip&#41;</Function>
     <Function>handleSelectRow&#40;oRow&#41;</Function>
     <Function>handleClick&#40;&#41;</Function>
     <Function>function anonymous&#40;&#41;&#10;&#123;&#10;handleClick&#40;&#41;&#10;&#125;</Function>
     </CallStack>
     </ScriptErrorDetails>
     <ClientInformation>
     <BrowserUserAgent>Mozilla&#47;4.0 &#40;compatible&#59; MSIE 7.0&#59; Windows NT 6.0&#59; SLCC1&#59; .NET CLR 2.0.50727&#59; Media Center PC 5.0&#59; .NET CLR 3.0.04506&#59; .NET CLR 3.5.21022&#59; MS-RTC LM 8&#41;</BrowserUserAgent>
     <BrowserLanguage>en-us</BrowserLanguage>
     <SystemLanguage>en-gb</SystemLanguage>
     <UserLanguage>en-gb</UserLanguage>
     <ScreenResolution>1440x900</ScreenResolution>
     <ClientName>Web</ClientName>
     </ClientInformation>
     <ServerInformation>
     <OrgLanguage>1033</OrgLanguage>
     <OrgCulture>2057</OrgCulture>
     <UserLanguage>1033</UserLanguage>
     <UserCulture>2057</UserCulture>
     <OrgID>&#123;8E5EB426-352A-402D-A3BA-85BE88096C04&#125;</OrgID>
     <UserID>&#123;CB375E51-65F2-DC11-9260-0003FF69D9A1&#125;</UserID>
     <CRMVersion>4.0.7333.3</CRMVersion>
     </ServerInformation>
    </CrmScriptErrorReport>



    Also, in few custom forms we have added two IFrames to display the values... One IFrame have to display the master records and the other have to display it's relevant records... That's, if the user chooses one record in the top IFrame, bottom IFrame have to refresh on it's own and should display the relevant records based on the selection of master record.

    Typical example is, in top frame if I select an Account bottom frame have to display the list of Contacts that are associated with the selected Account...
    Monday, March 31, 2008 2:38 PM
  • Anybody watching this thread? Any  samples or links for referring?
    Tuesday, April 1, 2008 12:22 PM
  •  

    Hi.

     

    The code below binds to the crmGrid inside an IFRAME.

    Change the iframe name to yours.

     

    var IFRAME_TEST = 'IFRAME_TEST';

     

    function OnCrmPageLoad()

    {

       //onclick event

       BindToCrmGridRow();

    }

     

    function GetIFrame( iframeId ){

          return document.getElementById(iframeId);

    }

     

    function GetCrmGrid( iframeId ){

          return GetIFrame( iframeId ).document.frames[iframeId].document.all.crmGrid

    }

     

    function BindToCrmGridRow()

    {

        var contactIframe = GetIFrame(IFRAME_TEST);

                contactIframe.attachEvent( "onreadystatechange" , OnIframeReady );

    }

     

    function OnIframeReady()

    {

          var contactIframe = GetIFrame(IFRAME_TEST);

          if( contactIframe.readyState != 'complete' )

                return;

               

          var crmGrid = GetCrmGrid(IFRAME_TEST);

                crmGrid.attachEvent( "onclick" , OnCrmGridClick );

    }

     

    function OnCrmGridClick()

    {

          var crmGrid = GetCrmGrid(IFRAME_TEST); 

          // SelectedRecords[0] contains all the information you need and more...

          alert( crmGrid.InnerGrid.SelectedRecords[0][0] );

    }

     

    Cheers,

    Adi

    Friday, April 4, 2008 2:10 AM
  • Hi Adi,

    Thanks a lot for the sample... This works great if I click on the grid (without adding icon in the grid)... Will try to add an icon in the grid and will check the onclick event and let you know how it works...
    Friday, April 4, 2008 2:34 PM
  • where to put this code in order to execute it.

    i have a requirement where i need to generate an alert when user selects a record and tries to delete.


    Regards
    Thursday, November 19, 2009 9:42 AM
  • Hi Adi,

    I am wondering if you can help me. I am placing the view into the IFRAME using this code (from Jim Wang):

    var navId = "nav_ava_ava_activityassociation_phonecall";
    if(document.getElementById(navId) != null){
      var tmp = document.getElementById(navId).onclick.toString();
      tmp = tmp.substring(tmp.indexOf("'")+1, tmp.indexOf(";"));
      var loadArea = tmp.substring(0, tmp.indexOf("'"));
       var roleOrd =  (tmp.indexOf("roleOrd") == -1) ? -1 : tmp.substring( tmp.indexOf("roleOrd"), tmp.lastIndexOf("'")).replace("\\x3d", "="); 
       crmForm.all.IFRAME_AssociatedTo.src = (roleOrd == -1) ? GetFrameSrc(loadArea) : GetFrameSrc(loadArea) + "&" + roleOrd;
    }

    This is loading the viiew into the IFRAME fine.

    I am then using your functions as above and the following code to call them

    var IFRAME_TEST = 'IFRAME_AssociatedTo';
    OnCrmPageLoad() ;

    The problem I am having is in the function (which fires off as expected whrn I click on the grid)

    function OnCrmGridClick()

    {

          var crmGrid = GetCrmGrid(IFRAME_TEST); 

          // SelectedRecords[0] contains all the information you need and more...

          alert( crmGrid.InnerGrid.SelectedRecords[0][0] );

    }

    the crmGrid variable populates fine with an object but crmGrid.InnerGrid is returning undefined.

    Are you able to gice me any pointers on what might me causing this? I have tried with both 1:N and N:N relationships and get the same error.

    Thanks in advance
    Rob


    Rob Francis
    Wednesday, February 10, 2010 9:52 AM