业务流程错误:Dynamics CRM和Visual Studio

时间:2014-10-22 13:53:15

标签: dynamics-crm

我已根据多个字段编写了一个C#插件,用于更新父记录。 在这里,我试图根据子实体中更新的值计算父实体中的总值,子实体中包含rate和units字段。所以基本上,总=率*单位。代码构建正常,但是当在dynamic crm中创建一个新表单时,它会导致业务流程错误:插件中的意外异常(执行):Parentchild1.parentchildpluginSystem.Collections.Generic.KeyNotFoundException:字典中没有给定的键

这是我的代码:

命名空间Parentchild1 {     public class parentchildplugin: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);

        }

1 个答案:

答案 0 :(得分:0)

你要求的问题:

  

字典中没有给定的密钥。

这是因为如果您尝试获取类似

的属性,则一个键不在列表中

int number = (int)entity.Attributes["random_attribute"]

这会引发错误,因为random_attribute不在上下文中。

你必须确保属性在上下文中......最好的做法就是要求包含:

if (entity.Contains("random_attribute"))

通过这种方式,您可以安全地访问该属性。

另一个原因可能是Image,请确保它在上下文中。

相关问题