Custom code in “Convert To Case” OOB ribbon button

Today we got a requirement to perform some logic on the create of case by using “Convert To Case” button on email activity. The logic shouldn’t execute if the case is created manually (Service –> Case –>  +New). I decided to write a plugin on create of case and perform my logic but the problem is to identify whether the case is created manually or using “Convert To Case” button. Case record is not holding any reference of source email to distinguish. After some research I decided to write a plugin on “Update” of “Email” entity (filtering attribute: regardingobjectid). Whenever an email is converted to case, the platform updating the regarding field with the case that is newly created. So I felt this way I can identify the case which is created by using “Convert To Case” button. However, my plugin executes whenever some one update the regarding field manually on email entity, so I used the following conditions in my plugin to avoid this scenario.

  • Depth should be more than 1
  • Regarding shouldn’t hold any value in PreImage and should hold the value in PostImage
  • “Subject” of email should be same as the “Title” of the case     

Code

              IOrganizationService organizationService = localContext.OrganizationService;
            ITracingService tracingService = localContext.TracingService;
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity targetEntity = (Entity)context.InputParameters["Target"];
                var targetRegardingObject = targetEntity.Contains("regardingobjectid") ? targetEntity.GetAttributeValue<EntityReference>("regardingobjectid") : null;
                if (targetRegardingObject != null && targetRegardingObject.LogicalName == "incident")
                {
                    Entity preImage = (Entity)context.PreEntityImages["PreImage"];
                    Entity postImage = (Entity)context.PostEntityImages["PostImage"];
                    var preRegarding = preImage.Contains("regardingobjectid") ? preImage.GetAttributeValue<EntityReference>("regardingobjectid") : null;
                    var postRegarding = postImage.Contains("regardingobjectid") ? postImage.GetAttributeValue<EntityReference>("regardingobjectid") : null;
                    var postSubject = postImage.Contains("subject") ? postImage.GetAttributeValue<string>("subject") : string.Empty;
                    var incidentRecord = organizationService.Retrieve("incident", targetRegardingObject.Id, new ColumnSet("title"));
                    var caseTitle = incidentRecord != null && incidentRecord.Contains("title") ? incidentRecord.GetAttributeValue<string>("title") : string.Empty;

                    if (context.Depth > 1 && preRegarding == null && postRegarding != null && caseTitle == postSubject)
                    {
                        //Your code is here
                    }
                }
            }

The above code is perfectly working for the scenario that I explained above but I am happy to hear suggestions and any scenarios that I have not considered.  

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s