Quantcast
Channel: Hiding in Plain Sight » customizations
Viewing all articles
Browse latest Browse all 5

Getting the Opportunity Entity in Plugin when Closing as Won

$
0
0

I had an interesting situation that I had to overcome when writing a plugin for Microsoft Dynamics CRM 2011. When an Opportunity was Closed as Won, I needed my plugin to generate a new Account Number if one didn’t already exist on the account. I thought this would be fairly straightforward to do. Just register the plugin on the SetState and SetStateDynamicEntity, add a Post Image for the Opportunity, and go from there. To my surprise, it didn’t work. My plugin wasn’t getting fired at all on those messages. So, what message was I supposed to use?

After searching the issue, I found this blog entry: http://mscrmshop.blogspot.com/2008/06/opportunity-setstate-plugin-problems.html

I tried a lot of different things and at the end I registerfed my pluggin for win and lose message instead of setstate or setstatedynamicentity message. It worked this time.

So, there was a Win and a Lose message. As I was only concerned about the Win, I altered my plugin to be registered on the Win message. That was when I encountered my second issue. I could not register a post image on the Win message. How was I going to find the Account ID if I couldn’t get to the entity? So, I threw in a bunch of tracing information to dump the information from the context, to see what values I had available to me, and forced the plugin to throw an error so I could get to the tracing information. What I discovered was that, in the Input Parameters, there was an OpportuntityClose entity in there, that contained the Entity Reference to the original Opportunity Entity. That was just what I needed. So, my next step was to write a simple function where I could retrieve the OpportuntityId from the OpportunityClose Input Parameter.

private Guid GetOpportunityId(IPluginExecutionContext context)
{
    Guid opportunityId = Guid.Empty;

    if (context.InputParameters.Contains("OpportunityClose") &&
        context.InputParameters["OpportunityClose"] is Entity)
    {
        Entity entity = (Entity)context.InputParameters["OpportunityClose"];

        if (entity.Attributes.Contains("opportunityid") &&
           entity.Attributes["opportunityid"] != null)
        {
            EntityReference entityRef = (EntityReference)entity.Attributes["opportunityid"];

            if (entityRef.LogicalName == "opportunity")
            {
                opportunityId = entityRef.Id;
            }
        }               
    }

    return opportunityId;
}

With the Opportunity Id in hand, I was able to load the Opportunity Entity, read the Customer ID (to get the Account), and generate the required Account Number if necessary.


Filed under: Microsoft Dynamics CRM Tagged: CRM, crm issues, customizations, Microsoft, microsoft dynamics crm, Microsoft Dynamics CRM 2011, Plugin, technology

Viewing all articles
Browse latest Browse all 5

Trending Articles