Search This Blog

Thursday, October 20, 2022

Microsoft Dynamics CRM Plugin Best Practices

 Here are some practices which developers can adopt to write efficient Dynamics CRM Plugin.

1. Entity Check

Always check if the plugin is fired from your target entity e.g if you want  your plugin to be executed from the Account Entity you can specify following check to ensure that.

1
2
3
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "account")
return;

2. Write Plugins that execute Faster

Performance is the key factor when dealing with Microsoft Dynamics CRM plugins, so always write plugins that works efficiently and don’t take much time to perform their intended task. Data retrieval is the key factor that determine the execution time of  Dynamics CRM Plugin.

When using service.Retrieve or service.RetrieveMultiple, always retrieve the minimum amount of data your plugin needs. You can achieve by specifying column set.Only specify those column which you need. Do not retrieve all columns of entity.

1
2
ColumnSet attributes = new ColumnSet(new string[] { "firstname", "lastname" });
account = service.Retrieve(account.LogicalName, _accountId, attributes);

3. Exception Handling

Always use try catch block in your plugin code for handling the exceptions.

1
2
3
4
5
6
7
8
Try
{
//Implement Your Logic here !!
}
catch (Exception ex)
 {
 throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
 }

4. Filtering Attributes

Filtering attributes in Plugin registration tool tell us the attributes which trigger the plugin. So selecting all attributes is not advisable because it will execute your plugin even if you don’t need it.

filering attribute in dynamics crm plugin

So only select filtering attributes that trigger your plugin.

 

filering attribute in dynamics crm plugin

5. Update Same Entity at Pre Update

If you want to  make changes in the same entity then best practice is to register your plugin at PreUpdate/Pre Create instead of using Service.Update.

1
2
3
4
Entity entity = (Entity)context.InputParameters["Target"];
String fname = (String)entity["fname"];
String lname = (String)entity["lname"];
entity["fullname"] = fname + lname;

6. Use Images instead of Retrieve

Use Images ( Pre or Post) instead of retrieve where applicable, it will make plugin execution faster as retrieving record from server takes time.

7. Parameters In Images

When dealing with images in plugins do not pass all attributes as parameters in images, select those parameters only which you want to use in your plugin.

 

parameters in dynamics crm plugin images

8. User’s Context

Any business logic executed within a plug-in, including Web service method calls and data retrieval, is governed by the security privileges of User on behalf of which the plugin is executed so when dealing with complex queries in Microsoft Dynamics CRM Plugins where you have to retrieve data from different entities if you select “Run in User’s Context”  as calling User it may result in exception if calling user doesn’t have enough security privileges to access those entities or records.

user context in dynamics crm plugin

So to resolve this issue Impersonation is used to execute Plugins on behalf of a Microsoft Dynamics CRM system user to provide desired feature or service for that user with all security privileges.

9. Debugging in Dynamics CRM Plugin

Use tracing in Dynamics CRM plugin for debugging purposes.

1
2
3
ITracingService tracingService =
 (ITracingService)serviceProvider.GetService(typeof(ITracingService));
tracingService.Trace("Plugin Message" + context.MessageName.ToUpper());

After the Spring Update 1 of CRM 2015 you can write and check these trace logs in Plugin Tracing Logs.

For using this feature you have to enable Plugin Tracing Logs in your organization.

To enable Plugin tracing logs follow the following steps.

  • Select Settings and under the System menu, Select Administration.
  • On Administration page click on System Settings and navigate to Customization tab.
  • Change “Enable logging to plug-in tracing log” to All or Exception.

Enable Dynamics CRM Plugin tracing

 

To open Plugin Tracing Logs select Settings and under the Customization menu, select Plug-In Trace Logs.

tracing in dynamics crm plugins



No comments:

Post a Comment