【发布时间】:2014-10-22 13:53:15
【问题描述】:
我编写了一个 C# 插件,用于基于多个字段对父记录进行后期更新。 在此,我试图根据子实体中更新的值计算父实体中的总值,子实体中包含比率和单位字段。所以基本上,总=比率*单位。代码构建良好,但是在动态 crm 中创建新表单时,它会生成业务流程错误:插件出现意外异常(执行):Parentchild1.parentchildpluginSystem.Collections.Generic.KeyNotFoundException:字典中不存在给定的键.
这是我的代码:
命名空间 Parentchild1 { 公共类父子插件:IPlugin { 私有实体 abcevent_parent;
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Get a reference to the Organization service.
IOrganizationServiceFactory factory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters != null)
{
//entity = (Entity)context.InputParameters["Target"];
//Instead of getting entity from Target, we use the Image
Entity entity = context.PostEntityImages["PostImage"];
Money rate = (Money)entity.Attributes["abcevent_rate"];
int unit = (int)entity.Attributes["abcevent_unit"];
// EntityReference parent = (EntityReference)entity.Attributes["abcevent_parentid"];
//Multiply
// Money total = new Money(rate.Value * units);
//Set the update entity
Entity parententity = new Entity("abcevent_parent");
//parententity.Id = parent.Id;
//parententity.Attributes["abcevent_total"] = total;
// abcevent_parentid = Guid IOrganizationservice.Create(Entity parentid);
Guid parentGuid = service.Create(abcevent_parent);
EntityReference parent = (EntityReference)entity.Attributes["abcevent_parentid"];
Money total = new Money(rate.Value * unit);
//Update
//service.Update(parententity);
}
【问题讨论】:
标签: dynamics-crm