D365 CE - Plugin - Set Attributes of Case Based on the Parent Email Activity (Convert to Case)
In the realm of Dynamics 365 CE customisation, specific client requirements often demand tailored solutions. Recently, a client approached us with a nuanced request: they wanted the Reported On (Custom) field of Case record to be dynamically set based on the creation method—either OOB Created On if case is created manually by the user or Created On of the parent Email Activity if the Case is created using 'Convert to Case' option from an Email activity.
To achieve this precise functionality, numerous approaches exist. However, the most robust method involves the creation of a custom Plugin. This post serves as a guide detailing the steps to implement a Plugin in Dynamics 365 CE that fulfills this specific client requirement.
The primary strategy involves writing a custom Plugin registered at the Pre-Operation stage for the Create message of the Incident entity. This stage ensures access to essential attributes before data persistence in the database.
I am assuming that you are already familiar with developing custom Plugins for Dynamics 365 CE. Therefore, this article focuses on key functions and essential code segments necessary to accomplish the above requirement.
context.ParentContext.ParentContext will contain the parent Email Activity. Now retrieve the parent Email Activity using the ActivityId and then set the Reported On field of the Incident to OOB Created On field of the retrieved Email activity. Below code implements this logic.

Thank you for reading this blog post!
To achieve this precise functionality, numerous approaches exist. However, the most robust method involves the creation of a custom Plugin. This post serves as a guide detailing the steps to implement a Plugin in Dynamics 365 CE that fulfills this specific client requirement.
The primary strategy involves writing a custom Plugin registered at the Pre-Operation stage for the Create message of the Incident entity. This stage ensures access to essential attributes before data persistence in the database.
I am assuming that you are already familiar with developing custom Plugins for Dynamics 365 CE. Therefore, this article focuses on key functions and essential code segments necessary to accomplish the above requirement.
Step-by-Step Guide:
Step 1: Create a Class Library Project and Implement IPlugin Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CaseOnCreateSetReportedOn : IPlugin | |
{ | |
public void Execute(IServiceProvider serviceProvider) | |
{ | |
try | |
{ | |
} | |
catch (Exception ex) | |
{ | |
throw new InvalidPluginExecutionException(ex.Message); | |
} | |
} | |
} |
Step 2: Generate Early Bound Classes for Incident and Email (Activity)
The next step is to generate Early Bound Classes for Incident and Email activity. You can use EarlyBoundGenerator tool that is available via XRMToolBox. The good practice is to generate classes only for the entities that are required for the code. For this particular requirement, I needed to generate classes only for Incident and Email activity. Make sure you also generate code for OptionSets.Step 3: Get OOB CreatedOn Field of the Incident Entity
In the Execute method of your Plugin Class, retrieve the Target Entity and check if the MessageName is 'Create.' Set the Reported On field of the Incident record to OOB Created On field of the Incident, fulfilling the initial requirement.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Entity entity = (Entity)context.InputParameters["Target"]; | |
entity.Attributes["xxx_reportedon"] = entity.Attributes["createdon"]; |
Step 4: Handle 'Convert to Case' Scenario
After the initial code, verify if context.ParentContext.ParentContext is not null. In the situation of Case creation via the 'Convert to Case' button from an Email activity, context.ParentContext.ParentContext will not be Null. Alternatively, you can check the context. Depth; it will be 2 instead of 1 in this scenario.context.ParentContext.ParentContext will contain the parent Email Activity. Now retrieve the parent Email Activity using the ActivityId and then set the Reported On field of the Incident to OOB Created On field of the retrieved Email activity. Below code implements this logic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//If Case is created from an Email Activity | |
if (context.ParentContext.ParentContext != null) | |
{ | |
if (context.ParentContext.ParentContext.MessageName == "ConvertActivity") | |
{ | |
Guid parentEmailActivityId = new Guid(context.ParentContext.ParentContext.InputParameters["ActivityId"].ToString()); | |
EmailHelper helper = new EmailHelper(organizationService, tracer); | |
Email parentEmaillActivity = helper.GetEmailActivity(parentEmailActivityId); | |
if (parentEmaillActivity != null) | |
{ | |
//Set the case's reportedon field to createdon date of the parent Email activity | |
//xxx is my solution publisher prefix | |
entity.Attributes["xxx_reportedon"] = parentEmaillActivity.CreatedOn; | |
} | |
} | |
} |
Step 5: Register your Plugin
Now finally register your plugin for the Create message of Incident entity as Pre-Operation Synchronous plugin.Complete Code
The complete code of the CaseOnCreateSetReportedOn Plugin and the EmailHelper class is below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CaseOnCreateSetReportedOn : IPlugin | |
{ | |
public void Execute(IServiceProvider serviceProvider) | |
{ | |
try | |
{ | |
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); | |
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); | |
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); | |
IOrganizationService organizationService = factory.CreateOrganizationService(context.UserId); | |
Entity entity = (Entity)context.InputParameters["Target"]; | |
if (context.MessageName == "Create") | |
{ | |
tracer.Trace("Context Message Name: " + context.MessageName); | |
tracer.Trace("Entity Logical Name: " + entity.LogicalName); | |
tracer.Trace("Entity ID: " + entity.Id); | |
entity.Attributes["m3d_reportedon"] = entity.Attributes["createdon"]; | |
//If Case is created from an Email Activity | |
if (context.ParentContext.ParentContext != null) | |
{ | |
if (context.ParentContext.ParentContext.MessageName == "ConvertActivity") | |
{ | |
Guid parentEmailActivityId = new Guid(context.ParentContext.ParentContext.InputParameters["ActivityId"].ToString()); | |
EmailHelper helper = new EmailHelper(organizationService, tracer); | |
Email parentEmaillActivity = helper.GetEmailActivity(parentEmailActivityId); | |
if (parentEmaillActivity != null) | |
{ | |
//Set the case's reportedon field to createdon date of the parent Email activity | |
entity.Attributes["xxx_reportedon"] = parentEmaillActivity.CreatedOn; | |
} | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw new InvalidPluginExecutionException(ex.Message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailHelper | |
{ | |
protected ITracingService tracer; | |
protected IOrganizationService organizationService; | |
public EmailHelper(IOrganizationService Service, ITracingService trace) | |
{ | |
organizationService = Service; | |
tracer = trace; | |
} | |
public Email GetEmailActivity(Guid emailActvityId) | |
{ | |
Email email= organizationService.Retrieve(Email.EntityLogicalName, emailActvityId, new ColumnSet("createdon")).ToEntity<Email>(); | |
return email; | |
} | |
} |
Conclusion
Custom Plugins in Dynamics 365 CE empower developers to address intricate business requirements precisely. By implementing this custom Plugin, the Reported On field in Cases seamlessly aligns with the client's needs, ensuring data accuracy and efficiency.Thank you for reading this blog post!
Comments
Post a Comment