Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

  • gdas Profile Picture
    gdas 50,085 on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

    Hi ,

    Have a look my blogs -

    community.dynamics.com/.../what-is-stopping-you-to-use-out-of-the-box-subgrid-in-dynamics-365

  • meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

    Hey, this solution doesn't work in Unified Interface. Any solution please ?

  • Nehal Gaikwad Profile Picture
    Nehal Gaikwad 35 on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

    Hi, but how do you attach your fetchXML resultset to the subgrid on the form using RetrieveMultiple plugin?

  • Suggested answer
    Silent Serenity Profile Picture
    Silent Serenity 55 on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

    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!

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

    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

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

    Hi Alex,

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

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

    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
    Community Member Microsoft Employee on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

    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;
    
                       }                    
    
                   }
    
               }
    
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics 365 Online : Applying custom FetchXml to a subgrid using JavaScript

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans