Debugging a Dynamics 365 CRM Plugin

Debugging

Debugging is the process of identifying and resolving errors in a system. Though it is a complex and time-consuming task, it is necessary to debug the code and ensure the functionality of the application. Because a single bug can cause a drastic impact, it might be an iterative process and might require multiple attempts to track and resolve the bugs.

Debugging a Dynamics 365 CRM plugin

Dynamics 365 CRM plugins are developed in C#(.NET). Plugins are of two types, which are synchronous and asynchronous. We cannot debug the plugin on the runtime; rather, we can do it using a profiler. A profiler will capture the instance of the plugin execution and save it as a row in the plugin-profiler table in Dataverse. You need the Plugin Registration tool to debug the Dataverse plugins.

There are multiple ways in debugging Dynamics 365 CRM Plugins.

  • Trace Logs
  • Persist to Entity
  • Exception

I have used create email activity plugin to demonstrate the debugging of the plugin. This plugin creates an email activity when a contact record gets created. After the development, build the plugin and sign the assembly.

Trace Logs

We can log the errors using the ITracingService interface in the plugin. This trace log accepts only the string to log. It is used for debugging both synchronous and asynchronous plugins. If you want to log values of any other data type, you should typecast it to string. This will log the errors, and it will appear on the Plugin Trace logs. Before that, you should make sure that your environment enabled the Plugin Trace logs.

ITracingService log = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
log.Trace("Execution Started");

To do so, you should navigate to Settings 🡲 Advanced Settings 🡲 Administration 🡲 System Settings 🡲 Customization 🡲 Enable Plug-in and custom workflow activity tracing to All (if not enabled).

Debug Dynamics 365 CRM Plugin

  • To see the Trace logs, navigate to Settings 🡲 Advanced Settings 🡲 Plug-In Trace log.
    Debug Dynamics 365 CRM Plugin
  • Once we hit the Plug-In Trace log, it will take us to the Plugin Trace Logs grid view page. Click on any of the record, you will get to see the trace logs which are logged during the execution of Plugin code.
    Debug Dynamics 365 CRM Plugin
  • Open any of the records; you will see the configuration and execution details. In the execution section, you will see the message block which we logged while developing a plugin.
    Debug Dynamics 365 CRM Plugin

Persist to Entity (Recommended)

This is the recommended way of debugging Dynamics 365 CRM plugins. In this method, we can use the saved profiles from the plugin profiles table to debug the plugin code. It is used for debugging both synchronous and asynchronous plugins. In this method, we can catch the profiler once the whole process is completed.

Follow the below steps to debug the plugin using Persist to Entity

Debug Dynamics 365 CRM Plugin

  • In Visual Studio, place some break points in the code to catch the debugger.
    Debug Dynamics 365 CRM Plugin
  • Go to Debug 🡲 Attach to Process and select Plugin Registration Tool, and hit Attach.
    Debug Dynamics 365 CRM Plugin
  • For that, we have to register our plugin in Plugin Registration Tool. Create a new plugin assembly by uploading the .dll file from the files.
    Debug Dynamics 365 CRM Plugin
  • Create a new step under the plugin by adding a message, entity, event execution, and type.
    Debug Dynamics 365 CRM Plugin
  • Select the step and click on the Start Profiling button on the navigation bar. A dialog box prompts up and asks for specifying profiler storage option.
    Debug Dynamics 365 CRM Plugin
  • Select persist to Entity as a profile storage option and click on OK.
    Debug Dynamics 365 CRM Plugin
  • Once the plugin got profiled, you will see the text profiled near your plugin step.
  • Then perform the actual scenario to capture the profile. In my context, I have created a new contact record.
    Debug Dynamics 365 CRM Plugin
  • Click on debug in Plugin Registration Tool and click on the down arrow symbol to add the captured Profile.
    Debug Dynamics 365 CRM Plugin
  • Add the assembly and click on the start execution button.
    Debug Dynamics 365 CRM Plugin
  • Once you click Start Execution, it will navigate to the Visual Studio and stops at the breakpoint. Then you can move forward by clicking the F10 key. To check the values for the variables, you can hover over the variable and make sure the value is correct or change the code to make it right.
    Debug Dynamics 365 CRM Plugin

Exception

The exception is another way to debug the Dataverse plugins. It will use the exception file as the profile. In this method, we cannot proceed with the whole action even if it is executed in the right way. Because of the system throws an exception and stops the process. This method is used for debugging the synchronous plugins.

Follow the below steps to debug the plugin using the Exception

Note. I have used the same plugin which is demonstrated in Persist to entity method

  • Place the breakpoints on the code in Visual Studio. Go to Debug, 🡲 Attach to Process, and Select Plugin Registration Tool.
  • In the Plugin Registration tool, select the step and click on Start Profiling.
    Debug Dynamics 365 CRM Plugin
  • Select Exception for profile storage and click OK.
  • Go to the Application and perform the action to trigger the plugin.
  • In my context, I have to create a contact to trigger the plugin. I will get the business process error with the exception file in it.
    Debug Dynamics 365 CRM Plugin
  • Click on the download log file, and you will get the .txt file.
  • Go to the Plugin Registration Tool and click on Debug and select the downloaded .txt file and add the respective assembly.
    Debug Dynamics 365 CRM Plugin
  • Click on the Start Execution. It will navigate to Visual Studio and hit the breakpoint.
  • You can move the debugger using the F10 key and hover over the variables to check the values.

Summary

Thus, the above-mentioned ways are used for debugging the plugins. But Microsoft recommends using persist to the entity for debugging. Trace logs will be used for logging the values. As programming is not a one-time activity, it requires a lot of debugging to make the right application.

Have a great day!


Similar Articles