Include DESCRIPTION field from Close Opportunity in an email when opportunity is closed as lost/won
I recently got a requirement from one of our clients where every time they close an opportunity as LOST they wanted to send an email through a workflow and include the description of why the opportunity was lost.
See below where we are closing an opportunity as LOST .
Now, when we close it , it pops us with a separate window for close opportunity as shown below:-
We were trying to achieve this with out of the box workflow feature in CRM and every time we receive an email the description used to come as NULL. Soon, we isolated that the description field where we enter the data when an opportunity is lost, is not the same “description” field that you see in Opportunity entity record form and add in your OOB workflow.
This description field is stored in ActivityPointerBase and not opportunity base and the entity we refer here is CLOSE OPPORTUNITY.
Researched and tried few things further and it was confirmed that this is not possible through OOB workflows to fetch this description field that you fill when an opportunity is lost .
We need to develop a custom plugin to achieve this requirement.
This is because the Opportunity entity has a 1:N relationship with the Opportunity Close entity, and you cannot access the 'N' (related) records through the workflow.
SOLUTION
- Thus, we created a very simple logic as how we can achieve this. Most of you must have had already achieved it but thought of sharing it on public forum so that it can help everyone.
- Firstly, we need to add one extra field to the Opportunity Entity –say new_closingdescription.
- The Close Description field is to be added to the “Closed Opportunities” or “Won Opportunities” or “Lost Opportunities” views when you want to test if your code worked.
- Note that these fields do not need to be present on the Opportunity form.
- Next we need to create a simple plug-in which will run on "post-create" of an "Opportunity Close" record to copy the data from the Opportunity Close to the related Opportunity record.
- I have tested it in CRM 2013 and it works fine
- If you want to make it work for CRM 2015 make sure you add the dlls from CRM 2015 SDK as in my case I have added CRM 2013 SDK
- Below is the code for the plugin :-
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Microsoft.Mervynr.OpportunityClose
{
public class OptyClose: IPlugin
{
void IPlugin.Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
Entity entity = (Entity)context.InputParameters["Target"];
var description = entity.GetAttributeValue<string>("description");
var opportunityId = ((EntityReference)entity.Attributes["opportunityid"]).Id;
Entity opportunity = service.Retrieve("opportunity", opportunityId, new ColumnSet(new string[] { "new_closingdescription" }));
opportunity["new_closingdescription"] = description;
service.Update(opportunity);
}
}
}
I have also attached my complete solution in this blog , if you would like to directly exact the dll and the field name created is new_closingdescription
I hope this blog as well helps others who are trying to achieve this and are not developers so that you can directly use this dll , register it in CRM and start working :)