2013-10-18 25 views
0

我是你在CRM插件開發中稱之爲「n00b」的東西。我正在嘗試爲Microsoft Dynamics CRM 2011編寫一個插件,當您創建新聯繫人時,該插件將創建一個新的活動實體。我希望這個活動實體與聯繫實體相關聯。CRM2011 - 「給定的密鑰不在字典中」

這是我當前的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xrm.Sdk; 

namespace ITPH_CRM_Deactivate_Account_SSP_Disable 
{ 
    public class SSPDisable_Plugin: IPlugin 
    { 

     public void Execute(IServiceProvider serviceProvider) 
     { 

      // Obtain the execution context from the service provider. 
      IPluginExecutionContext context = (IPluginExecutionContext) 
       serviceProvider.GetService(typeof(IPluginExecutionContext)); 

      IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
      IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 

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

      if (entity.LogicalName != "account") 
      { 
       return; 
      } 

      Entity followup = new Entity(); 
      followup.LogicalName = "activitypointer"; 
      followup.Attributes = new AttributeCollection(); 
      followup.Attributes.Add("subject", "Created via Plugin."); 
      followup.Attributes.Add("description", "This is generated by the magic of C# ..."); 
      followup.Attributes.Add("scheduledstart", DateTime.Now.AddDays(3)); 
      followup.Attributes.Add("actualend", DateTime.Now.AddDays(5)); 

      if (context.OutputParameters.Contains("id")) 
      { 
       Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString()); 
       string regardingobjectidType = "account"; 

       followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid); 

      } 

      service.Create(followup); 

     } 


    } 

} 

但是,當我嘗試運行這段代碼:我得到一個錯誤,當我嘗試創建在CRM環境中一個新的聯繫人。錯誤是:「給定的密鑰不在字典中」(鏈接* 1)。當我嘗試保存新聯繫人時,該錯誤彈出正確。

鏈接* 1:http://puu.sh/4SXrW.png
(翻譯粗體文字: 「業務流程錯誤」)

+0

我認爲錯誤是'activitypointer',你需要指定確切活動實體類型你想創建(任務,約會,...) –

+0

任何建議,我怎麼做?我是一名CRM插件開發的初學者。我能得到的所有幫助都非常感謝。 –

+0

您的意思是您希望在用戶創建聯繫人時創建活動,但在代碼中您正在查找「帳戶」實體。 – Scorpion

回答

2

的Microsoft Dynamics CRM使用術語活動來介紹幾種類型的相互作用。活動類型爲: 電話,任務,電子郵件,信件,傳真和預約。

ActivityPointer (Activity) Entity

爲了使你的代碼工作替換以下行:

Entity followup = new Entity(); 

Entity followup = new Entity("task"); 

並刪除以下行:

followup.LogicalName = "activitypointer"; 

也請閱讀我的評論和Guido Preite的上述表揚。您需要修改代碼以使其與聯繫人一起工作。

編輯

確保ContactId並在CRM它引用到活動前就存在。

+0

抱歉,這並未解決問題。我仍然收到與以前一樣的錯誤消息。有沒有更多的相關數據可以讓我們更容易發現可能的錯誤? –

+0

您可以確認該插件是在後期還是在舞臺上註冊。我認爲context.OutputParameters [「ID」]是不是在系統中,你要創建活動的時間存在。 – Scorpion

+0

我相信後我有。 [添加程序集](http://puu.sh/4T0du.png)[添加步驟](http://puu.sh/4T0fC。png) –

0

這經常會發生,如果你在你的插件在那裏它已經被添加的顯式添加屬性值的目標實體。

不是entity.Attributes.Add(...)

使用實體[ 「爲AttributeName」] = ...

相關問題