0

我实现了我的第一个 LINQ 查询来检查重复记录,同时用户添加新记录但它没有被解雇

我正在研究 CRM2011,我使用 LINQ 编写了插件并使用插件注册工具进行了注册

下面是我的代码

 if (context.Depth == 1)
        {
            if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
            {
                target =(Entity)context.InputParameters["Target"];
                if (target != null)
                {
                    householdname = target.GetAttributeValue<string>("mcg_HouseholdName");
                }
            }
            OrganizationServiceContext orgcontext = new OrganizationServiceContext(service);
            {
                var query = (from c in orgcontext.CreateQuery<mcg_household>()
                             where c.mcg_HouseholdName == householdname
                             select c
                            );
                List<mcg_household> householdlist = query.ToList<mcg_household>();
                if (householdlist.Count > 0)
                {
                    throw new InvalidPluginExecutionException("Record Already Exists!");
                }
            }
        }

我认为问题出在 getattribute 上,因为当我用一些硬编码值检查它时,它会运行。请告诉我应该在哪个阶段注册这个插件以及代码是否有任何问题。

4

1 回答 1

2

如果您的代码适用于硬编码示例,则可能是执行阶段的问题。您必须在执行的预操作阶段和同步模式中注册您的插件步骤。查看这篇文章了解详情

另外,检查“mcg_HouseholdName”是否是正确的字符串。

于 2013-11-01T09:39:37.470 回答