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.

Monday, October 25, 2021

How to restrict future date in MS CRM 365

Recently our client has came with the requirement that, they don’t want to allow a future date selection for a particular field on Microsoft Dynamics CRM form.

Well, it is not a complex task to implement. Here is the sample code for it-

function restrictFutureDate() {

    if (Xrm.Page.getControl("Date_Field")) {
        var getfieldValue = Xrm.Page.getAttribute("Date_Field").getValue();
   
        var today = new Date();
   
        if (getfieldValue > today) {
   
            Xrm.Page.getControl("Date_Field").setNotification("Future date is not allowed!", "DATEERROR");
        } else {
   
            Xrm.Page.getControl("Date_Field").clearNotification("DATEERROR");
        }

    }
}

Saturday, October 23, 2021

Set Lookup Value in MS CRM 365 using JavaScript

 

While working on client-side customization using Java Script in MS CRM 365, it is very common requirement to set the Lookup field value which can be done using following code-

Here is the sample function which can be use to set the Lookup filed value-

function setLookupField(executionContext,fieldName, recordId,
recordName, entitySchemaName) {
    var formContext = executionContext.getFormContext();
    var lookupData = new Array();
    var lookupItem = new Object();
    lookupItem.id = recordId;
    lookupItem.name = recordName;
    lookupItem.entityType = entitySchemaName;
    lookupData[0] = lookupItem;
formContext.data.entity.attributes.get("parentcontactid").setValue(lookupData);

}

 

The code demonstrate itself that it is not enough to set the record id to the field, it is necessary to set record id, name and entityType then the value will reflect correctly in the field which is placed on the form.

 

Hope this will help.

Get Lookup Value in MS CRM 365 using JavaScript

 

While working on client-side customization using Java Script in MS CRM 365, it is very common requirement to get the Lookup field value which can be done using following code-

Let’s look into the code step by step-

First of all we have to get the entity object using formContext.data.entity.attributes.get() Method

E.g.

var ObjAccount = formContext.data.entity.attributes.get("accountid");  

 

Once the object is available in the variable ObjAccount we can use GetValue() method as shown below-

E.g.

var ObjAccountValues = ObjAccount.getValue();

 

GetValue() Metho return the id, name and entityType properties which can we accessed as shown in full function written to get the Looup Values

function LookupValueFctn(executionContext) {
    var formContext = executionContext.getFormContext();
var ObjAccount=formContext.data.entity.attributes.get("accountid");  

    if (ObjAccount != null) {
        var ObjAccountValues = ObjAccount.getValue();

        if (ObjAccountValues != null) {
            var Accountid = ObjAccountValues[0].id;  
            var AccountNameName = ObjAccountValues[0].name;
            var EntitySchemaName =ObjAccountValues[0].entityType;
        }
    }
}

 

Tuesday, October 12, 2021

How to pass execution context from Ribbon Workbench in MS CRM 365

As Microsoft has deprecated Xrm.Page, we have recently required to correct our JavaScript code where we use Xrm.page.

For example we have used Xrm.page.data.save method multiple places in our JavaScript customization code, which we have to replace with formContext.data.save. The JavaScript functions which are getting called from the form or field event it is easy to pass the execution context.

In case of the functions which are getting called on click of Ribbon button is explicitly requires to pass the CRM parameter using Ribbon Workbench customization, below are the steps which needs to follow the same-

 

1.     Load the solution in Ribbon Workbench

2.     Choose to modify the Command for this custom Name button and click on Add Parameter


3.     After selecting the Add Parameter, select CRM Parameter:


 

4.     Select Primary Control from the dropdown


 

Now you can get formContext from this parameter as easily as if it were the executionContext.

Here is code example if you are calling function on lick on Form Ribbon button

function ribbonButtonClick(executionContext)

{

       executionContext.data.save();

}

 


Monday, October 11, 2021

Retrieve Record Using XRM.WebAPI

In Dynamics 365 version it is very easy to retrieve the entity record in JavaScript is quite simple and not requires writing multiple lines of code.

On success it returns a promise containing a JSON object with the retrieved columns and their values.

Here a basic retrieve example-

Retrieves the name and revenue of an account record with record ID = 5531d753-95af-e711-a94e-000d3a11e605.

Xrm.WebApi.retrieveRecord("account", "a8a19cdd-88df-e311-b8e5-6c3be5a8b200", "?$select=name,revenue").then(

    function success(result) {

        console.log("Retrieved values: Name: " + result.name + ", Revenue: " + result.revenue);

        // perform operations on record retrieval

    },

    function (error) {

        console.log(error.message);

        // handle error conditions

    }

);

The above example displays the following in your console; you might see other values depending on your data:

Retrieved values: Name: Sample Account, Revenue: 5000000


Reference:

https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrieverecord


Wednesday, October 6, 2021

Tooltip for label in not showing after upgrading to release 2021 Wave 2

We have recently applied Release 2021 Wave 2 to our MS CRM 365 instances and found that it is not showing Tooltip for the labels which we configured, isnt it strange behaviour?

Tried to find out reasong behing it and found that Microsoft has deprecated the Tooltip feature in this release with below description provided by Microsoft-


Tooltips for column descriptions on model-driven forms

With the upcoming 2021 release wave 2 (public preview in August 2021 and GA in October 2021), hover tooltips on column labels for full descriptions will be removed.

Why is this needed?

  • These tooltips aren't an accessible way to access information about the column.

Impact

  • Starting October 2021, these descriptions will no longer be available on hover.
  • We will be introducing a new fully accessible feature for April 2022.

Hope this will help.

Reference-
https://docs.microsoft.com/en-us/power-platform/important-changes-coming

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