简体   繁体   English

CRM 2011 Online C#检查机会是否获胜

[英]CRM 2011 Online C# Check if Opportunity is Won

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. 据我了解,您无法在CRM在线部署中使用机会赢得的插件消息。

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. 我用Google搜索,似乎无法找到解决方案。 Can anyone explain what I need to do here? 任何人都可以解释我在这里需要做什么吗?

Thanks in Advance. 提前致谢。

(OptionSetValue)entity.Attributes["statuscode"];

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 name property. 换句话说,如果更新商机名称,则会触发插件,但属性包仅包含name属性。

So the statuscode here is not being passed in, and hence that code fails because, as the exception says, it isn't in the dictionary. 所以statuscode这里没有被传递,因此该代码失败,因为,作为异常说,它是不是在字典。

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. 但最简单(但不一定最有效)的方法是回调CRM以获取价值。

var entity = service.Retrieve(Target.LogicalName, Target.Id, new ColumnSet(true));
var entityStatusCode = (OptionSetValue)entity.Attributes["statuscode"];
if (entityStatusCode.Value == 3)
{
    //Code to execute if opportunity won
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM