Wednesday, January 25, 2023

Convert FetchXML to QueryExpression?

FetchXML and QueryExpression are two different query languages used to retrieve data from Microsoft Dynamics CRM. FetchXML is an XML-based query language that is used to retrieve data in a more simplified way, while QueryExpression is a more powerful and complex query language that provides more options and capabilities.

In some cases, you may need to convert FetchXML to QueryExpression in order to take advantage of the additional capabilities provided by QueryExpression. Fortunately, this process can be accomplished relatively easily using the FetchXmlToQueryExpressionRequest class provided by the CRM SDK.

Here's an example of how to convert FetchXML to QueryExpression using the FetchXmlToQueryExpressionRequest class:

using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + "<entity name='account'>" + "<attribute name='name'/>" + "<attribute name='address1_city'/>" + "<filter type='and'>" + "<condition attribute='address1_city' operator='eq' value='Seattle'/>" + "</filter>" + "</entity>" + "</fetch>"; FetchXmlToQueryExpressionRequest request = new FetchXmlToQueryExpressionRequest() { FetchXml = fetchXml }; FetchXmlToQueryExpressionResponse response = (FetchXmlToQueryExpressionResponse)service.Execute(request); QueryExpression query = response.Query;

In this example, a FetchXML query is created to retrieve all accounts located in Seattle. The FetchXmlToQueryExpressionRequest class is then used to convert the FetchXML query to a QueryExpression object. The resulting QueryExpression object can then be used to retrieve the data from the CRM using the RetrieveMultiple method.

It's important to keep in mind that, the conversion process may not always result in an exact match, as some FetchXML features may not have a direct equivalent in QueryExpression. Additionally, some of the complexities of FetchXML may get lost in the conversion process.

In conclusion, converting FetchXML to QueryExpression is a process that can be accomplished relatively easily using the FetchXmlToQueryExpressionRequest class provided by the CRM SDK. This process allows you to take advantage of the additional capabilities provided by QueryExpression while still using the simplicity of FetchXML. However, it's important to keep in mind that the conversion process may not always result in an exact match and some of the complexities of FetchXML may get lost in the conversion process.

Tuesday, January 24, 2023

Pass Parameters to Forms in MS CRM

 In Microsoft Dynamics CRM, you can pass parameters to forms by using the "OpenForm" function in JavaScript. This function allows you to open a form and pass in any number of parameters to pre-populate the fields on the form.

Here's an example of how you might use the "OpenForm" function to open a contact form and pre-populate the "Fullname" field:

var parameters = {}; parameters["fullname"] = "John Smith"; Xrm.Navigation.openForm({ entityName: "contact", useQuickCreateForm: false, parameters: parameters });

In this example, we first create an object called "parameters" and add a key-value pair to it with the key "fullname" and the value "John Smith". Next, we call the "openForm" function and pass in the entity name "contact" and the parameters object. The "useQuickCreateForm" parameter set to false, it will open the main form of the entity.

You can also pass multiple parameters by adding more key-value pairs to the "parameters" object:

var parameters = {}; parameters["fullname"] = "John Smith"; parameters["email"] = "johnsmith@example.com"; parameters["phone"] = "123-456-7890"; Xrm.Navigation.openForm({ entityName: "contact", useQuickCreateForm: false, parameters: parameters });

It's important to note that the parameter name must match the logical name of the attribute in the CRM, otherwise the parameter will not be populated.

You can also pass parameters to the forms while opening it via the command bar or ribbon buttons. You need to define the function to pass the parameters and call it on the button.

In summary, passing parameters to forms in Microsoft Dynamics CRM is a useful way to pre-populate fields on a form and save time for the user. You can use the "OpenForm" function in JavaScript and pass in any number of parameters to pre-populate the fields on the form. Additionally, you can also pass parameters via command bar or ribbon buttons by defining the function to pass the parameters and calling it on the button.

Monday, January 23, 2023

How to mock ITracingService

In Microsoft Dynamics CRM, the ITracingService interface is used to log diagnostic information from plugins and custom workflows. To mock the ITracingService interface in a plugin, you can create a test class that implements the ITracingService interface and overrides the methods that you want to test. Here is an example of how to create a mock ITracingService class in C#:

public class MockTracingService : ITracingService { public void Trace(string format, params object[] args) { // Your implementation to trace the message } }

ou can then use this mock class in your plugin test code to test the plugin's behavior when it calls the ITracingService methods. For example, you can use the Assert class to verify that the plugin calls the Trace method with the expected message.

[TestMethod] public void TestPlugin() { var mockTracingService = new MockTracingService(); var plugin = new MyPlugin(mockTracingService); plugin.Execute(...); Assert.AreEqual("Expected message", mockTracingService.TracedMessage); }

t's important to note that, depending on your needs, you can also use a mocking framework such as Moq, NSubstitute and RhinoMocks to mock the ITracingService interface, this allows you to verify that the methods were called, how many times they were called, among other things.

Keep in mind that the above example is a simple example to understand the concept, you should adapt it to your specific needs and requirements.

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