locked
Fetch XML filter in subgrid RRS feed

  • Question

  • Hi,

    I want to Control the filter in a subgrid and tried to modify the code from this
    https://community.dynamics.com/crm/b/magnetismsolutionscrmblog/archive/2013/01/24/filter-sub-grid-dynamics-crm-2011-rollup-12-polaris.aspx

    I have modified to filter on the current record to the fetch XML filter but it dosent work, am I doing something wrong?

    Appriciate any suggestion! :)

    My code:

    function filterSubGrid() {
        var befattningarGrid = document.getElementById("zync_befattningsrelation"); //grid to filter 
    	
        if (befattningarGrid == null) { //make sure the grid has loaded 
            
    		setTimeout(function () { updateSubGrid(); }, 2000); //if the grid hasn’t loaded run this again when it has 
            return;
        }
    	var guid = Xrm.Page.data.entity.getId();
    	
    	var namn = Xrm.Page.data.entity.attributes.get("name").getValue();
        //fetch xml code which will retrieve all the accounts related to the contact 
    	
        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
        "  <entity name='zync_befattningsrelation'>"+
        "    <attribute name='zync_kontaktperson' />" +
        "    <attribute name='zync_befattning' />" +
        "    <attribute name='zync_befattningsrelationid' />" +
        "    <order attribute='zync_befattning' descending='false' />" +
        "    <filter type='and'>" +
        "      <condition attribute='statecode' operator='eq' value='0' />" +
        "    </filter>" +
        "    <link-entity name='contact' from='contactid' to='zync_kontaktperson' visible='false' link-type='outer' alias='a_220024a485a2e2118b5cb499bafdfe63'>" +
        "      <attribute name='telephone1' />" +
        "      <attribute name='mobilephone' />" +
        "      <attribute name='emailaddress1' />" +
        "    </link-entity>" +
        "    <link-entity name='account' from='accountid' to='zync_konto' alias='ab'>" +
        "      <filter type='and'>"+
    	"        <condition attribute='parentaccountid' operator='eq' uiname='" + namn + "' uitype='account' value='" + guid + "' />" +
    	"      </filter>" +
    	"    </link-entity>" +
    	"  </entity>" +
    	"</fetch>";
    	
        befattningarGrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid   
        befattningarGrid.control.refresh(); //refresh the sub grid using the new fetch xml 
    }  

    Thursday, April 11, 2013 4:32 PM

All replies

  • You've used the wrong method name in your setTimeout..... updateSubGrid.  Shouldn't it be filterSubGrid?
    Friday, April 12, 2013 9:56 AM
  • Yes, I figured that out eventually. I’m not so good at this. J


    Everything works but nothing shows in the subgrid? I can see that it reloads when the scripts run but it gets no data.

    I have created the FetchXML in Advance search and copied it directly into the script. Do I need to make some changes to the FetchXML before I use it from the advance find?

    I would really appreciate some help with this.

    Friday, April 12, 2013 10:13 AM
  • Maybe try removing the uiname parameter from the fetch xml.

    So it looks like this 

    "      <filter type='and'>"+
    	"        <condition attribute='parentaccountid' operator='eq' uitype='account' value='" + guid + "' />" +
    	"      </filter>" +

    Friday, April 12, 2013 10:32 AM
  • Maybe try removing the uiname parameter from the fetch xml.

    So it looks like this 

    "      <filter type='and'>"+
    	"        <condition attribute='parentaccountid' operator='eq' uitype='account' value='" + guid + "' />" +
    	"      </filter>" +


    Tried it now and there is no dirrerence. No new error or any data in the subgrid??
    Friday, April 12, 2013 11:32 AM
  • check this relationship - 

       "    <link-entity name='contact' from='contactid' to='zync_kontaktperson' visible='false' link-type='outer' alias='a_220024a485a2e2118b5cb499bafdfe63'>" +
        "      <attribute name='telephone1' />" +
        "      <attribute name='mobilephone' />" +
        "      <attribute name='emailaddress1' />" +
        "    </link-entity>" +
        "    <link-entity name='account' from='accountid' to='zync_konto' alias='ab'>" +
        "      <filter type='and'>"+
    	"        <condition attribute='parentaccountid' operator='eq' uiname='" + namn + "' uitype='account' value='" + guid + "' />" +
    	"      </filter>" +
    	"    </link-entity>" +

    This is how I configured fetchxml query to retrieve cases for related contact based on its primaycustomer i.e. account . try building your fetchXml using advance find.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">

      <entity name="incident">

        <attribute name="title" />

        <attribute name="ticketnumber" />

        <attribute name="createdon" />

        <attribute name="incidentid" />

        <order attribute="title" descending="false" />

        <link-entity name="contact" from="contactid" to="customerid" alias="ac">

          <link-entity name="account" from="primarycontactid" to="contactid" alias="ad">

            <filter type="and">

              <condition attribute="accountid" operator="eq" uiname="Litware, Inc. (sample)" uitype="account" value="{F11F4FC7-079E-E211-B101-78E3B511A692}" />

            </filter>

          </link-entity>

        </link-entity>

      </entity>

    </fetch>


     

    http://mscrmkb.blogspot.com

    Mark as answer if a post has answered the question

    Friday, April 12, 2013 2:47 PM
  • check this relationship - 

       "    <link-entity name='contact' from='contactid' to='zync_kontaktperson' visible='false' link-type='outer' alias='a_220024a485a2e2118b5cb499bafdfe63'>" +
        "      <attribute name='telephone1' />" +
        "      <attribute name='mobilephone' />" +
        "      <attribute name='emailaddress1' />" +
        "    </link-entity>" +
        "    <link-entity name='account' from='accountid' to='zync_konto' alias='ab'>" +
        "      <filter type='and'>"+
    	"        <condition attribute='parentaccountid' operator='eq' uiname='" + namn + "' uitype='account' value='" + guid + "' />" +
    	"      </filter>" +
    	"    </link-entity>" +

    This is how I configured fetchxml query to retrieve cases for related contact based on its primaycustomer i.e. account . try building your fetchXml using advance find.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">

      <entity name="incident">

        <attribute name="title" />

        <attribute name="ticketnumber" />

        <attribute name="createdon" />

        <attribute name="incidentid" />

        <order attribute="title" descending="false" />

        <link-entity name="contact" from="contactid" to="customerid" alias="ac">

          <link-entity name="account" from="primarycontactid" to="contactid" alias="ad">

            <filter type="and">

              <condition attribute="accountid" operator="eq" uiname="Litware, Inc. (sample)" uitype="account" value="{F11F4FC7-079E-E211-B101-78E3B511A692}" />

            </filter>

          </link-entity>

        </link-entity>

      </entity>

    </fetch>


     

    http://mscrmkb.blogspot.com

    Mark as answer if a post has answered the question

    The fetch XML I have is created from Advanced Find.

    I want to retreive records with a relation to account which have the same parent-account as the same current account. Like the Parent/Child relation Account and Contact have. You can see in the parent account all the Contacts which related account have the current account as its parent account.

    Possible?

    Excuse the bad English, in a hurry. :)

    Friday, April 12, 2013 6:44 PM
  • I am not able to understand here.. if you are able to see records using advance find then you should be able to see the same records in the subgrid as well

    http://mscrmkb.blogspot.com

    Mark as answer if a post has answered the question

    Tuesday, April 16, 2013 3:24 AM
  • I am not able to understand here.. if you are able to see records using advance find then you should be able to see the same records in the subgrid as well

    http://mscrmkb.blogspot.com

    Mark as answer if a post has answered the question

    I agree, but I dont get the subgrid code to work? It shows nothing while the advance find show the correct data records....?

    Is there something I missing from the link?

    https://community.dynamics.com/crm/b/magnetismsolutionscrmblog/archive/2013/01/24/filter-sub-grid-dynamics-crm-2011-rollup-12-polaris.aspx

    Tuesday, April 16, 2013 7:12 AM