I need to check if an opportunity has been won when a plugin fire, before it executes any code. As I understand it, you can’t use the opportunity-won plugin message on CRM online deployments.
Here is a snippet of my code:
//Get the target entity
Entity entity = (Entity)context.InputParameters["Target"];
OptionSetValue entityStatusCode =
(OptionSetValue)entity.Attributes["statuscode"];
if (entityStatusCode.Value == 3)
{
//Code to execute if opportunity won
}
This throws the error ‘The given key is not in the dictionary’. I’ve googled around and I can’t seem to find a solution. Can anyone explain what I need to do here?
Thanks in Advance.
I am going to guess that line is giving you the error.
When a plugin fires, by default it only gives you the properties that have been changed in the event that invoked the plugin.
In other words, if you update an opportunity name, a plugin is fired but the properties bag will only include the
nameproperty.So the
statuscodehere is not being passed in, and hence that code fails because, as the exception says, it isn’t in the dictionary.As for how to fix it, kinda depends on why you need to check if the Opportunity is won. But the easiest (but not necessarily most efficient) way is to call back to CRM to pick up the values.