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.

Step-by-Step Guide:

Step 1: Create a Class Library Project and Implement IPlugin Interface


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.


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.



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:

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

Popular posts from this blog

D365 CE - Customer Voice - Get Customer Feedback and Calculate CSAT (Customer Satisfaction) Scores of Your Customer Service Agents

The new Not-to-Exceed feature in Dynamics 365 Field Service - Release 2023 Wave 1