“entity.Attributes.Contains”方法永远不会返回true

时间:2014-05-29 08:43:00

标签: c# crm dynamics-crm-2013

尝试在事件实体更新事件中创建CRM插件。

以下是插件的完整代码

namespace MyPluginPackage.Plugins{
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

public class PreValidateCaseUpdate: Plugin
{
    public PreValidateCaseUpdate()
        : base(typeof(PreValidateCaseUpdate))
    {
        base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(10, "Update", "incident", new Action<LocalPluginContext>(ExecutePreValidateCaseUpdate)));
    }

    protected void ExecutePreValidateCaseUpdate(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;
        if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity && context.Depth < 2)
        {
            Entity entity = (Entity)context.InputParameters["Target"];
            try
            {

                if (entity.Attributes.Contains("customerid"))
                {
                    //Some code. For example updates incident entity description
                    entity["description"] = "Some description";
                }

                service.Update(entity);
            }
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }
        }
    }
}}

问题是 if(entity.Attributes.Contains(“customerid”))永远不会返回TRUE。 “customerid”是事件实体的标准属性。任何想法和建议都非常感谢。

描述了我使用的部署插件的方法 in this blog post:

1 个答案:

答案 0 :(得分:0)

更新哪个字段调用插件执行?如果未更新customerid - 它将不会出现在Target中,因此您应该使用Images。我相信我已经在其他论坛上写过了......