[英]CRM 2011: How to Update record in a Create Plugin?
我在如何更新剛剛使用其他數據創建的記錄方面遇到了嚴重問題。
案例:我有短信活動。 在創建短信記錄。 插件會自動發送短信。 第三方短信提供商負責發送並返回狀態字符串。 根據這些字符串,必須更新sms的狀態。
這是我的一些代碼:
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(
typeof(IOrganizationServiceFactory));
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
aContext orgContext = new aContext(service);
Entity sms = (Entity)context.InputParameters["Target"];
/// logic goes here
sms.StatusCode = new OptionSetValue(statuscode); //statuscode is integer
service.Update(sms);
}
每次執行插件時,插件中都出現錯誤。 有人可以幫助並解釋我在這里做錯了什么嗎?
謝謝!
由於您的插件是同步執行的,因此如果您使用Pre-operation執行階段,則應該很容易更新您的實體。
在這種情況下,您可以執行以下操作:
Entity sms = (Entity)context.InputParameters["Target"]
// additional code to retrieve status
if(sms.Attributes.Contains("statuscode"))
sms.Attributes["statuscode"] = new OptionSetValue(statuscode);
else
sms.Attributes.add("statuscode", new OptionSetValue(statuscode));
這樣,您只需從Attributes dictionnary中添加或更新一個鍵,它就會被保存,好像該值直接來自表單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.