Introduction
When implementing Dynamics 365 projects, writing plugins is a common way to extend functionality and meet customized requirements. If there are multiple developers working on the same project, plugin comments are essential. Correctly commenting plugins can help others better understand the purpose and functionality of the code.
Below is a comment template for Dynamics 365 plugins, along with some sample code. This is the template I usually use. If there are any immature aspects in this template, feedback and corrections are welcome.
/// <summary>
/// <list type="bullet">
/// <item>Module: Pre-sales (example)</item>
/// <item>Function</item>
/// <list type="number">
/// <item>Function description xxxx xxxx xxxx</item>
/// <item>Function description xxxx xxxx xxxx</item>
/// </list>
/// <item>Primary Entity: Lead</item>
/// <item>Message: Update</item>
/// <item>Update Attributes: qualifyingopportunityid, statecode</item>
/// <item>State: PostOperation</item>
/// <item>Mode: Synchronous</item>
/// </list>
/// </summary>
Message/State/Mode Options
Message
Options- Create
- Update
- Delete
- Associate
- Disassociate
- SetStateDynamicEntity
- RetrieveMultiple
- Retrieve
- QualifyLead
- Assign
State
Options- Pre-validation
- Pre-operation
- Post-operation
Mode
Options- Synchronous
- Asynchronous
Example
using Microsoft.Xrm.Sdk;
using System;
namespace Blog.D365.Plugins.Account
{
/// <summary>
/// <list type="bullet">
/// <item>Module: Pre-sales (example)</item>
/// <item>Function</item>
/// <list type="number">
/// <item>Function description xxxx xxxx xxxx</item>
/// <item>Function description xxxx xxxx xxxx</item>
/// </list>
/// <item>Primary Entity: Lead</item>
/// <item>Message: Update</item>
/// <item>Update Attributes: qualifyingopportunityid, statecode</item>
/// <item>State: PostOperation</item>
/// <item>Mode: Synchronous</item>
/// </list>
/// </summary>
public class AccountPostUpdate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
IOrganizationService serviceAdmin = factory.CreateOrganizationService(null);
Entity currentEntity = (Entity)context.InputParameters["Target"];
// Add your plugin logic code here
}
}
catch (Exception ex)
{
tracer.Trace($"AccountPostUpdate unexpected exception:\n{ex.Message}");
throw;
}
}
}
}
Comments