Sunday, October 15, 2023

How to Apply Custom FetchXML to a Subgrid using JavaScript in MS CRM 365

Microsoft Dynamics 365 is a versatile CRM platform that offers various customization options to tailor your CRM experience to the specific needs of your organization. One common customization requirement is the ability to apply custom FetchXML queries to subgrids within CRM forms. In this blog post, we will explore how to use JavaScript to apply custom FetchXML to a subgrid in MS CRM 365.

Understanding Subgrids in Dynamics 365

Subgrids are a fundamental component of CRM forms. They allow you to display related records from other entities within the parent entity's form. Subgrids are often used to display information such as related activities, associated contacts, opportunities, or any related data.

While Dynamics 365 allows you to configure default views for subgrids, there are scenarios where you need to apply more customized filters and sorting using FetchXML queries.

Prerequisites

Before we proceed, ensure that you have the following prerequisites in place:

  1. Access to a Dynamics 365 instance: You need to have access to a Dynamics 365 instance where you can perform customization.

  2. Development environment: You should have access to a development environment, such as Visual Studio, to write and test JavaScript code.

Steps to Apply Custom FetchXML to a Subgrid

Here are the steps to apply custom FetchXML to a subgrid in MS CRM 365 using JavaScript:

Step 1: Create Your FetchXML Query

First, create the custom FetchXML query that defines the filter criteria and sorting for the records you want to display in the subgrid. You can use the FetchXML Builder tool in Dynamics 365 to assist in building and testing your query.

Step 2: Add JavaScript to the CRM Form

  1. Open the CRM form where the subgrid is located in the Dynamics 365 customizations.

  2. Add a web resource or custom JavaScript file to the form. This JavaScript file will contain the code to apply your custom FetchXML query.

  3. In the JavaScript file, use the following code as a template to apply your custom FetchXML query:

javascript
// Define the FetchXML query 
var customFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + " <entity name='your_entity_name'>" + " <attribute name='your_attribute_1' />" + " <attribute name='your_attribute_2' />" + " <filter type='and'>" + " <condition attribute='your_filter_attribute' operator='eq' value='your_filter_value' />" + " </filter>" + " </entity>" + "</fetch>"
// Get the subgrid control on the form 
var subgridControl = Xrm.Page.getControl("your_subgrid_name"); 
// Set the FetchXML on the subgrid 
subgridControl.setParameter("fetchXml", customFetchXML); 
// Refresh the subgrid to apply the custom query 
subgridControl.refresh();

Step 3: Replace Placeholders

In the JavaScript code template, replace the following placeholders with your specific details:

  • your_entity_name: The name of the entity related to the subgrid.
  • your_attribute_1, your_attribute_2: The attributes you want to display in the subgrid.
  • your_filter_attribute: The attribute you want to filter by.
  • your_filter_value: The value you want to filter for.
  • your_subgrid_name: The name of the subgrid control on the form.

Step 4: Save and Publish Changes

After adding and configuring the JavaScript code, save your CRM form customizations and publish them to make the changes effective.

Testing and Verification

Now, when you open the CRM form, the subgrid should display records according to your custom FetchXML query, filtering and sorting the records as defined in your JavaScript code.

Conclusion

Customizing subgrids in Dynamics 365 using custom FetchXML queries and JavaScript can be a powerful tool to display the right set of related records in a tailored manner. This customization allows you to meet specific business requirements, presenting relevant data to users, and enhancing the overall CRM experience.

No comments:

Post a Comment

QueryExpression vs. FetchXML in MS CRM with C#

Microsoft Dynamics CRM (Customer Relationship Management) is a powerful platform that helps organizations streamline their business processe...