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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

(0) ShareShare
ReportReport
Posted on by

Hi,

The implementation of applying custom FetchXml to a subgrid has appeared to have changed from CRM 2011/13 to Dynamics 365. The change is with respect to GridControl.SetParameter().

I am having the following code, Could someone shed some light how I can achieve this. I have followed many articles talking about this same issue but nothing is working at the moment on Dynamics 365 Online.

So could someone please suggest me is there any alternative method to achieve the same functionality.

In my below code, I am trying to fetch all the email activities related to the account and showing on the Sub-grid which is on the account form.

//Shows only the PhoneCall activities related to Organisation
        //var allPhoneCallsGrid = window.parent.document.getElementById("AllPhoneCalls"); //Not supported by Microsoft
        //var allPhoneCallsGrid = document.getElementById("AllPhoneCalls"); //Not Supported by Microsoft
        var allPhoneCallsGrid = Xrm.Page.getControl("AllPhoneCallactivities"); //Sub-grid is on the Account Form
        if (allPhoneCallsGrid == null) {
            setTimeout(function () {
                AccountForm.AccountFormOnLoad();
            }, 2000); //if the grid hasn’t loaded run this again when it has
            return;
        }
        var accountId = Xrm.Page.data.entity.getId();        
        var allPhoneCallsfetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
       "  <entity name='activitypointer'>" +
       " <attribute name='activitytypecode' />" +
       "        <attribute name='subject' />" +
       "        <attribute name='ownerid' />" +
       "        <attribute name='prioritycode' />" +
       "        <attribute name='regardingobjectid' />" +
       "        <attribute name='activityid' />" +
       "        <attribute name='scheduledstart' />" +
       "        <attribute name='scheduledend' />" +
       "        <attribute name='statecode' />            " +
       "        <attribute name='community' />   " +
       "    <order attribute='modifiedon' descending='false' />" +
       "    <filter type='and'>" +
       "      <condition attribute='activitytypecode' operator='ne' value='4206' />" +
       "      <condition attribute='activitytypecode' operator='eq' value='4210' />" +
       "    </filter>" +
       "    <link-entity name='incident' from='incidentid' to='regardingobjectid' alias='ad'>" +
       "      <filter type='and'>" +
       "        <condition attribute='account' operator='eq' uitype='account' value='" + accountId + "' />" +
       "      </filter>" +
       "    </link-entity>" +
       "  </entity>" +
       "</fetch>";
        allPhoneCallsGrid.control.SetParameter("fetchXml", allPhoneCallsfetchXml); //Unable to get property 'SetParameter' of undefined or null reference
        //allPhoneCallsGrid.getGrid().setParameter("fetchXml", allPhoneCallsfetchXml);
        allPhoneCallsGrid.control.Refresh(); //refresh the sub grid using the new fetch xml

*This post is locked for comments

I have the same question (0)
  • ashlega Profile Picture
    34,477 on at

    Hi,

     this was never a supported method - for the list of supported methods, please check this link:

    msdn.microsoft.com/.../dn932126.aspx

     That said, you can probably find a way to make it work again.. And it can break again any time.. For a more permanent solution, I would suggest looking into creating a "pre-retrieve" multiple plugin which would replace the query on the server side, and you can identify the query you need to replace by adding some kind of special condition(for example, fieldX = 'abracadabra') to the view assigned to that grid

  • Community Member Profile Picture
    on at

    Thank you for replying to my query. Have you got any examples how to achieve it or could you please explain a bit more.

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    I was referring to this kind of technique:

    https://hachecrm2011.wordpress.com/2013/07/19/filtering-views-by-intercepting-retrieve-multiple-queries-with-a-plugin/

    https://nishantrana.me/2016/04/22/plugin-on-retrieve-multiple-in-crm-2015/

    You'll just need to make sure not to modify those queries for each and every view associated with the entity, so you can add a special condition and, then, verify if it's there in the plugin (then you can actually remove all query conditions and add your own)

    In the plugin, it would look more or less like this then (and you'll just need to ensure that view you are using for the subgrid has a condition for the selected attribute that has "ABRACADABRA" as a value)

    bool replace = false;
    foreach (var f in qe.Criteria.Filters)
    {
      foreach (var c in f.Conditions)
      {
        if (c.AttributeName == ".." && c.Values[0] == "ABRACADABRA")  
       {

          replace = true;
       }
      }
    }
    if (replace)
    {
       //UPDATE THE QUERY HERE
    }

  • Community Member Profile Picture
    on at

    Thank you very much. I will try to implement the above.

  • Community Member Profile Picture
    on at

    Hi Alex,

    I am writing the plugin as you suggested to show the activities (that are related to account cases) onto a subgrid which is on account form and my code is below

    Could you please tell me whether the below condition is correct or wrong...Many Thanks.

    // The InputParameters collection contains all the data passed in the message request.
    
               if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is QueryExpression && context.MessageName == "RetrieveMultiple")
    
               {
    
                   QueryExpression qe = (QueryExpression)context.InputParameters["Query"];                
    
                  if (qe.EntityName == "activitypointer")
    
                   {
    
                       bool replace = false;                    
    
                       string currentCustomerId = string.Empty;                    
    
                       foreach (var filter in qe.Criteria.Filters)
    
                       {
    
                           foreach (var condition in filter.Conditions)
    
                           {
    
                               if (condition.AttributeName == "account")                              
    
                                   currentCustomerId = condition.Values[0].ToString();
    
                               //if (!string.IsNullOrEmpty(currentCustomerId) && currentCustomerId == "Test")
    
                              // {
    
                                   replace = true;
    
                               //}
    
                           }
    
                       }
    
                       if (replace)
    
                       {
    
                           //UPDATE THE QUERY HERE
    
                           FetchXmlToQueryExpressionRequest req = new FetchXmlToQueryExpressionRequest();
    
                           req.FetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
    
                                          "  <entity name='activitypointer'>" +
    
                                          " <attribute name='activitytypecode' />" +
    
                                          "        <attribute name='subject' />" +
    
                                          "        <attribute name='ownerid' />" +
    
                                          "        <attribute name='prioritycode' />" +
    
                                          "        <attribute name='regardingobjectid' />" +
    
                                          "        <attribute name='activityid' />" +
    
                                          "        <attribute name='scheduledstart' />" +
    
                                          "        <attribute name='scheduledend' />" +
    
                                          "        <attribute name='statecode' />            " +
    
                                          "        <attribute name='community' />   " +
    
                                          "    <order attribute='modifiedon' descending='false' />" +
    
                                          "    <filter type='and'>" +
    
                                          "      <condition attribute='activitytypecode' operator='ne' value='4206' />" +
    
                                          "      <condition attribute='activitytypecode' operator='eq' value='4202' />" +
    
                                          "    </filter>" +
    
                                          "    <link-entity name='incident' from='incidentid' to='regardingobjectid' alias='ad'>" +
    
                                          "      <filter type='and'>" +
    
                                          "        <condition attribute='account' operator='eq' uitype='account' value='" + currentCustomerId + "' />" +
    
                                          "      </filter>" +
    
                                          "    </link-entity>" +
    
                                          "  </entity>" +
    
                                          "</fetch>";
    
                           FetchXmlToQueryExpressionResponse resp = (FetchXmlToQueryExpressionResponse)service.Execute(req);
    
                           context.InputParameters["Query"] = resp.Query;
    
                       }                    
    
                   }
    
               }
    
  • ashlega Profile Picture
    34,477 on at

    Hi Bellam,

     I think you should be using "regardingobjectid" for the attributename there to get currentcustomerid.

     Although, that way you'll intercept all activitypoonter views.

     That's why I'd also add a special condition(see posts above) to that view to identify a view that you actually need to replace

    PS.  You may have to do a bit of tracing in that plugin - to see attribute names etc.. Maybe use tracing service.. or just throw an InvalidPluginExecutionException to see what are the values of those filters/conditions.

  • Community Member Profile Picture
    on at

    Hi Alex,

    Many Thanks for your help. I am able to achieve the requirement

  • ashlega Profile Picture
    34,477 on at

    Hi Bellam,

     perfect!

     I suggested one of the posts above as an answer - if that's the approach you used, eventually, you might want to mark it as an answer.

  • Prashanth Kamasamudram Profile Picture
    337 on at

    Hi Alex,

     We have used same approach on activities subgrid  and attachments subgrids on contact form where there are three subgrids on form of which two are for activities and one for attachments, everything works fine but there is a issue with performence,  it takes around 40 to 50 seconds before loading these grids and other grids on form depending upon number of records. Is ther any way to optimize this. or any alternative way to achive this?

    Kind Regards

    Prashanth

  • Suggested answer
    Silent Serenity Profile Picture
    55 on at

    Hi Bellam,

    For D365 CRM V9.0, assuming you already set up, var formContext = executionContext.getFormContext();

    where executionContext was pass as a parameter in the function.

    using the code => formContext.getControl("subgrid name in the form") and use it as

    (variable name from the earlier code snipet).control.SetParameter("fetchXml", FetchXml); WILL NOT WORK! :(

    However, the only thing that works for me is:

    var subGrid = window.parent.document.getElementById("subgrid name in the form"); // Yes, I know DOM is not supported.

    And the rest of the code are pretty much the same.

    Happy Coding!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans