In this blog we will try to bring you some of the best practices that developers can adopt to write performance and feature effective Dynamics CRM Plugin.
- Entity Check
Before continuing development of Plugin, ensure that the fired event is for your entity (Check if Target is your entity). For example, if we are expecting Entity to be Account we can check target like this:
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "account")
2. Use common Methods
There are several methods that can be used to process different messages that can be trigger for plugins, but best practices recommend to use common methods like Create, Delete or Update instead Execute method. The reason is that Plugin should only be triggered for specific event, not for every general event. It consume less time and it is cost efficient
3. Use Early Bound Types
If your Plugin is well defined and you know all the entities that will be included in process it is the best way to use Early Bound Types. One advantage is that code’s compile verification check is done, so you will know all the errors before deployment of Plugin. You can generate Early Type Classes using CrmSvcUtil tool that comes with Dynamics CRM Development SDKs.
4. Exception Handling
Always use try catch block in your plugin so that execution of your plugin is controlled so that user does not experience unwanted errors which can affect all the other processes.
5. Update Same Entity
Best practices advise us that if you need to update same Entity to use Pre-Update Plugin which will not trigger this Plugin again and potential make loops which can broke execution of Plugin.
6. Debuggin in Dynamics CRM Plugin
Tracing (Introduced in Spring Update 1 of CRM 2015) is something that is included in Dynamics CRM and it provides good way of debugging Plugin. Other way of debugging code for earlier version of CRM is for async Plugins. In System Jobs you can find a lot of information regarding execution of your Plugin.