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.

Bulk Update of Dynamics 365 in ASP.NET Core

 Microsoft Dynamics 365 is a powerful CRM platform that allows organizations to manage customer data, automate processes, and drive business growth. Occasionally, you may need to perform bulk updates of records in Dynamics 365 to ensure data accuracy, implement changes across multiple entities, or for various other reasons. In this blog post, we will explore how to perform a bulk update of Dynamics 365 records using ASP.NET Core, leveraging the Web API.

Prerequisites

Before we dive into the bulk update process, ensure that you have the following prerequisites:

  1. Microsoft Dynamics 365 Instance: You should have access to a Dynamics 365 instance.

  2. ASP.NET Core Project: Create an ASP.NET Core project to host your application.

  3. Web API Authentication: Set up authentication for accessing the Dynamics 365 Web API. You can use OAuth, App Registration, or other suitable authentication methods.

Step 1: Set Up Your ASP.NET Core Project

If you haven't already, create an ASP.NET Core project using your preferred development environment and set up your authentication method to connect to the Dynamics 365 Web API. You may use libraries like Microsoft.Identity.Web for authentication.

Step 2: Connect to the Dynamics 365 Web API

Once your project is set up, you need to establish a connection to the Dynamics 365 Web API. The Web API is a RESTful endpoint that allows you to interact with Dynamics 365 data. You can use libraries like HttpClient to make HTTP requests to the API. Here's an example of how to connect:

csharp
using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; var client = new HttpClient(); client.BaseAddress = new Uri("https://<YourOrganization>.crm.dynamics.com"); client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0"); client.DefaultRequestHeaders.Add("OData-Version", "4.0"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Authenticate your request here (e.g., OAuth2). // Perform CRUD operations with Dynamics 365 data using the client.

Step 3: Prepare the Bulk Update Data

Before performing a bulk update, you need to prepare the data that you want to update. This data could be in the form of a list or a collection, depending on your use case. Ensure that the data is in a format that matches the records in your Dynamics 365 entity.

Step 4: Implement the Bulk Update

To perform the bulk update, loop through your prepared data and make HTTP requests to the Dynamics 365 Web API for each record. You will need to use the HTTP PATCH method to update records. The request should contain the record's GUID in the URL and the updated data in the request body.

Here's a simplified example of how to update a record using the Web API:

csharp
var recordId = "GUID-of-Record-To-Update"; var updateData = new { Field1 = "NewValue1", Field2 = "NewValue2", // Add more fields as needed. }; var content = new StringContent(JsonConvert.SerializeObject(updateData), Encoding.UTF8, "application/json"); var requestUri = $"api/data/v9.0/entitysetname({recordId})"; var response = await client.PatchAsync(requestUri, content);

Repeat this process for each record in your data collection. It's essential to handle errors, authentication, and response status codes appropriately to ensure the bulk update process runs smoothly.

Step 5: Error Handling and Logging

In a bulk update scenario, it's vital to handle errors gracefully. You should log any issues encountered during the update process and implement retry mechanisms if necessary. This ensures that your updates are as reliable as possible.

Conclusion

Performing a bulk update of Dynamics 365 records in an ASP.NET Core application requires a structured approach. By connecting to the Dynamics 365 Web API, preparing your data, and making HTTP requests for each record, you can efficiently update large volumes of data in your CRM system. Be sure to handle errors and ensure data integrity throughout the process, and you'll have a reliable and effective solution for bulk updates in Dynamics 365.

JavaScript Events and Functions of Business Process Flows in MS CRM 365

 Microsoft Dynamics 365 CRM offers a robust platform for managing customer relationships and streamlining business processes. One of the powerful features within Dynamics 365 CRM is Business Process Flows (BPFs). These BPFs enable organizations to create structured workflows to guide users through various stages of a process. To further enhance and customize these processes, JavaScript events and functions come into play. In this blog post, we will delve into the JavaScript events and functions that can be used with Business Process Flows in MS CRM 365.

Understanding Business Process Flows

Business Process Flows (BPFs) in Dynamics 365 CRM are sequences of stages and steps that users can follow to complete a particular process or workflow. These processes help ensure consistency and adherence to defined business rules. Some common examples of processes that can be modeled using BPFs include lead management, opportunity tracking, case resolution, and more.

BPFs consist of stages, each of which contains one or more steps. Users move through these stages by completing the required steps, and the stages represent the different phases of the process.

JavaScript Events in Business Process Flows

JavaScript events can be used to enhance the functionality of BPFs and to interact with the BPF control on forms. Here are some key events that you can use:

  1. OnStageChange: This event is triggered when the user switches from one stage to another. You can use this event to execute custom logic, such as validating data, sending notifications, or updating related records.
  2. OnStageSelected: This event is triggered when the user selects a specific stage from the dropdown list. It allows you to perform actions based on the user's stage selection.
  3. OnProcessStatusChange: This event is fired when the status of the BPF changes, for example, when it's marked as "Active" or "Completed." You can use this event to perform specific actions based on the process status.

JavaScript Functions for Business Process Flows

In addition to events, JavaScript functions are available to interact with Business Process Flows. These functions allow you to manipulate and control the flow of a BPF from your custom scripts. Here are some essential JavaScript functions for BPFs:

  1. Xrm.Page.data.process.getProcess: This function allows you to retrieve the current BPF instance. You can access information such as the current stage, active path, and more.

  2. Xrm.Page.data.process.setActiveProcess: You can use this function to set a specific BPF as the active one for a record. This can be useful if you want to switch to a different BPF programmatically.

  3. Xrm.Page.data.process.moveNext: This function enables you to programmatically move the BPF to the next stage. You can use this to automate stage transitions based on specific conditions.

  4. Xrm.Page.data.process.setActiveStage: Use this function to set a specific stage within the BPF as active. This is valuable for guiding users to specific stages during scripting.

Example Use Cases

Let's explore a couple of use cases to illustrate the application of JavaScript events and functions in BPFs:

Use Case 1: Automatic Stage Transition

Imagine you have a BPF for lead qualification in Dynamics 365 CRM. When a lead's score reaches a certain threshold, you want the BPF to automatically transition from the "Qualify" stage to the "Develop" stage. You can achieve this using the Xrm.Page.data.process.moveNext function within the "OnStageChange" event.

Use Case 2: Data Validation on Stage Change

In a service request BPF, you might want to validate whether all required information has been entered before allowing users to move from the "In Progress" stage to the "Resolved" stage. You can utilize the "OnStageChange" event to perform data validation and notify users of any missing information.

Conclusion

JavaScript events and functions play a pivotal role in enhancing the functionality of Business Process Flows in MS CRM 365. They allow you to automate, validate, and customize the BPF experience to align with your organization's specific processes and requirements. By leveraging these scripting capabilities, you can create more dynamic and user-friendly BPFs that help streamline your business operations and ensure consistency in your CRM processes.

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...