2012-06-21 91 views
0

我正在嘗試編寫我的第一個Dynamics Crm 2011插件。CRM 2011樣本 - 帳號生成錯誤

因此,我下載了CRM11 sdk並查看了插件示例。我終於使用提供的工具安裝並註冊了我的第一個插件(至少對我來說這是一個非常陡峭的學習曲線)。

我從基礎入手,編寫了樣本中提供的「賬號插件」,並使用了註冊工具。所有被報告爲安裝成功,但創建一個新的帳戶生成的帳號根本沒有顯示出來。我最初認爲插件沒有正確安裝,但在注意到代碼會拋出一個錯誤,如果帳號已經存在,我着手嘗試讓插件引發錯誤。這是第一次,所以我很高興安裝是正確的。

查看示例代碼後,我失去了如何將生成的帳號保存回Dynamis Crm。

// An accountnumber attribute should not already exist because it is system generated. 

if (entity.Attributes.Contains("accountnumber") == false)  
{  
    // Create a new accountnumber attribute, set its value, and add 
    // the attribute to the entity's attribute collection. 

    Random rndgen = new Random(); 
    entity.Attributes.Add("accountnumber", rndgen.Next().ToString());  
}  
else  
{  
    // Throw an error, because account numbers must be system generated.  
    throw new InvalidPluginExecutionException("The account number can only be set by the system.");  
} 

翻看一些其他樣本,我注意到一些額外的代碼似乎被稱爲創建新的實體。我調整了這一點並將其複製到新的帳號代碼中,並部署了帳號並按預期生成帳號。

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) 

    serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 

service.Update(entity); // Originally - service.Create(entity); 

這是示例代碼中的錯誤還是我做錯了什麼?如果實體向數據庫提交其更改時不需要此代碼?

回答

2

剛做完了。您需要將此插件註冊爲「預操作」步驟。

這將教我離開註冊工具的默認值。設置爲「預操作」後,實體的設置在保存之前會被更改。因此,您不需要再次調用保存。