Hi guys,
Here is a new post that tries to highlight how to write a
Dynamics CRM plugin on win of an opportunity. Yes, I had some issues while developing
the plugin and I want to share my learnings so that you know better.
The scenario that this plugin looks to solve is not
important as much as to understand how to get the relevant details (read: opportunity details for the opportunity that was won) from the
plugin is.
So the code:
public class PostDealWin: Plugin
{
public PostDealWin()
: base(typeof(PostDealWin))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Win", "opportunity", new Action<LocalPluginContext>(ExecutePostDealWin)));
}
protected void ExecutePostDealWin(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
var tracingService = localContext.TracingService;
var orgService = localContext.OrganizationService;
var context = localContext.PluginExecutionContext;
if (context.InputParameters.Contains("OpportunityClose") && context.InputParameters["OpportunityClose"] is Entity)
{
var opportunityCloseEntity = (Entity)context.InputParameters["OpportunityClose"];
if (opportunityCloseEntity != null && opportunityCloseEntity.Attributes.Contains("opportunityid") &&
opportunityCloseEntity.Attributes["opportunityid"] != null)
{
var dealEntityRef = (EntityReference) opportunityCloseEntity.Attributes["opportunityid"];
if (dealEntityRef != null)
{
try
{
// your magic comes here
}
catch (Exception ex)
{
tracingService.Trace("An exception occurred while trying to execute the post deal win plugin. Details: " + ex.Message);
throw;
}
}
}
}
}
}
public class PostDealWin: Plugin
{
public PostDealWin()
: base(typeof(PostDealWin))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Win", "opportunity", new Action<LocalPluginContext>(ExecutePostDealWin)));
}
protected void ExecutePostDealWin(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
var tracingService = localContext.TracingService;
var orgService = localContext.OrganizationService;
var context = localContext.PluginExecutionContext;
if (context.InputParameters.Contains("OpportunityClose") && context.InputParameters["OpportunityClose"] is Entity)
{
var opportunityCloseEntity = (Entity)context.InputParameters["OpportunityClose"];
if (opportunityCloseEntity != null && opportunityCloseEntity.Attributes.Contains("opportunityid") &&
opportunityCloseEntity.Attributes["opportunityid"] != null)
{
var dealEntityRef = (EntityReference) opportunityCloseEntity.Attributes["opportunityid"];
if (dealEntityRef != null)
{
try
{
// your magic comes here
}
catch (Exception ex)
{
tracingService.Trace("An exception occurred while trying to execute the post deal win plugin. Details: " + ex.Message);
throw;
}
}
}
}
}
}
The lines highlighted above are of interest since these are
the places where the code differs from the typical post update plugin.
So behind the hoods when the opportunity is won there is a “OpportunityClose”
entity (it is an activity entity - refer here: https://msdn.microsoft.com/en-us/library/gg334301.aspx)
that is passed to the plugin. It has a reference to the original opportunity
that is available in the opportunityid field. I am storing the opportunity
reference entity in the local variable dealEntityRef for use later. I then
perform a retrieve to get some details related to the opportunity entity using the
ID from this opportunity reference.
I have put the comment where you would want to do all your
magic after the opportunity is won!
I hope this helps someone! Happy Dynamics CRMing!
No comments:
Post a Comment