Sunday, April 17, 2016

Plugin Error to Entity Microsoft Dynamics CRM

Plugin(s) are used to apply custom business logic within organization service's event execution pipeline. In event of any exception the execution of plugin code terminates along with core operation. If you have enabled the exception logging of plugin exception (Settings>>Administration>>System Setting>>Customization>>Enable logging to Plugin Trace log >> Exception), plugin exception will be logged in Plugin Trace log.


Plugin Trace Log is a centralized view for all the plugin exceptions. Tracing of plugin exception to custom entity is not possible without customization and configuration. In this post we will see an approach to log plugin exception(s) to custom entity.



Requirement is to write a plugin on contact create, that plugin will validate "First Name", if first name is left blank then contact should not get created and plugin will throw error and terminates the core operation. And this specific plugin error needs to logged into "ContactPluginError" entity.




In above process flow chat, a recursive workflow will be running in background that will be triggered in every X minutes, it will create a custom entity record recursively, on that custom entity there will be a post operation registered plugin which will read & delete error detail from Plug-in trace log and move it to custom entity.

A. Enable Plug-in exception tracing

Plugin errors will be logged to Plug-in trace log when enabled from system settings. 



B. Custom Entity "ContactPluginError"

Create custom entity to log plug-in error. This entity will appear in Setting area.



Add fields to store exception details.




Customize "ContactPluginError" form to display error details.


Save and Publish customizations.


C. Custom Entity "RecursiveMovePluginError"

RecursiveMovePluginError entity will be created by workflow, this entity will have a plugin registered on create which will read data from plugin trace log entity and move it to custom entity. 


Add a field to store the type (plugin name) whose error log needs to moved from plugin trace log to ContactPluginError entity.



Customize RecursiveMovePluginError form and add field type.



Save and Publish Customization.

D. Create Workflow

Create background on demand workflow, that needs to be first triggered manually, once triggered it will automatically executes every 2 minutes and create RecursiveMovePluginError entity record.



Set below properties on Create step of RecursiveMovePluginError record.


Save and activate the workflow.

D. Plugin

Create and register "TracePlugin" plugin that will be move exception from Plugin trace log to custom entity.





Register plugin Post operation - Async



E. Execute and Trigger Process

Create Contact record without first name, an exception will the thrown by plugin

System plug-in trace log will be having an entry for this plugin error.



Configure and Trigger workflow job to move errors to ContactPluginError entity. Create a record for RecursiveMovePluginError and specify plugin type that needs to be monitored and moved.


Trigger workflow





Now all the plugin error thrown by plugin type "ContactDataValidation.FirstNamePlugin" will be moved from plugin trace log to custom entity "ContactPluginError".





Summary:

"Plugintracelog" entity does not expose "Create" message to register any plugin. Therefore when any record is created in "Plugintracelog"  entity there is no way to directly fire any plugin and perform any action on that.

Recursive workflows are auto triggered, so whenever a new record "RecursiveMovePluginError " is created, registered plugin "TracePlugin"  on "RecursiveMovePluginError" entity create message will also execute, its task is to read all the matched types "ContactDataValidation" plugin logs and move it to custom entity "ContactPluginError". Since I have configured workflow with a timeout condition of 2 minutes, so 2 minutes delay is expected in move operation from "PluginTracelog" entity to  "ContactPluginError" entity.


For Reference:

Solution File:




Plugin Code:


4 comments: