【问题标题】:Business Process Error: Dynamics CRM and Visual Studio业务流程错误:Dynamics CRM 和 Visual Studio
【发布时间】: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


    【解决方案1】:

    你问的问题:

    给定的键不在字典中。

    这是因为一个键不在列表中,如果您尝试获取类似的属性

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

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

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

    if (entity.Contains("random_attribute"))

    这样你就知道你可以安全地访问该属性了。

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

    【讨论】:

    • 另外,请确保您的帖子图片名称 PostImage 与插件注册工具中的帖子图片注册方式相匹配。
    • 试过了。现在有一个无效的参数异常。为父级创建 GUID 时出现错误异常
    • entity 将仅包含已更改的属性。我建议在 PostImage 中包含 abcevent_parentid 并从那里获取。
    • 你可以用你的实际代码更新你的帖子吗?我看到了很多评论,因此我们可以更好地帮助您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多