2013-01-11 23 views
0

我需要檢查插件是否在執行任何代碼之前觸發的機會。據我瞭解,您不能在CRM在線部署上使用商機獲得的插件消息。客戶關係管理2011在線C#檢查是否贏得機會

這裏是我的代碼片段:

//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 
} 

這引發錯誤「給定鍵不在字典」。我一直在搜索,我似乎無法找到解決方案。任何人都可以解釋我需要在這裏做什麼?

在此先感謝。

+0

你是怎麼註冊你的插件的? –

+0

此插件註冊了哪條消息,哪條線路正在拋出錯誤消息? – Nicknow

回答

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

我會猜測這條線給你的錯誤。

當一個插件觸發時,默認情況下它只會給你在調用插件的事件中已經改變的屬性。

換句話說,如果更新機會名稱,會激發一個插件,但屬性包只包含name屬性。

因此,statuscode這裏沒有被傳入,因此代碼失敗,因爲,正如例外說的,它不在字典中。

至於如何解決它,有點取決於爲什麼你需要檢查機會贏了。但最簡單的(但不一定是最有效的)方法是回電給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 
} 
相關問題