C# 更新的上下文消息不起作用

C# 更新的上下文消息不起作用,c#,visual-studio-2010,dynamics-crm-2011,C#,Visual Studio 2010,Dynamics Crm 2011,我正在开发一个CRM Dynamics插件。自定义实体上有一个名为“电子邮件”的字段。我想确保两个实体记录的电子邮件地址应该是唯一的。为此,我编写了以下代码: public class Class1 : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider.

我正在开发一个CRM Dynamics插件。自定义实体上有一个名为“电子邮件”的字段。我想确保两个实体记录的电子邮件地址应该是唯一的。为此,我编写了以下代码:

public class Class1 : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        // Obtain the execution context from the service provider.
        Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
            serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

        // Get a reference to the organization service.
        IOrganizationServiceFactory factory =
        (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = factory.CreateOrganizationService(context.UserId);


        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
        {
            Entity entity = (Entity)context.InputParameters["Target"];

            //</snippetAccountNumberPlugin2>

            // Verify that the target entity represents an account.
            // If not, this plug-in was not registered correctly.
            if (context.MessageName.ToUpper() == "CREATE")
            {
                if (entity.LogicalName == "new_assignment1entity")
                {
                    try
                    {
                        QueryExpression query = new QueryExpression("new_assignment1entity");
                        query.ColumnSet.AddColumns("new_email");
                        EntityCollection result1 = service.RetrieveMultiple(query);
                        foreach (var a in result1.Entities)
                        {
                            int size = result1.Entities.Count;
                            if (a.Attributes["new_email"].ToString().Equals(entity["new_email"]))
                                throw new InvalidPluginExecutionException("Duplicate Email found!");
                        }
                    }
                    catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
                    {
                        //You can handle an exception here or pass it back to the calling method.
                        throw new InvalidPluginExecutionException("Some problem occurred while Querying Records!");
                    }
                }
            }
            else if (context.MessageName.ToUpper() == "UPDATE")
            {
                if (entity.LogicalName == "new_assignment1entity")
                {
                    try
                    {
                        QueryExpression query = new QueryExpression("new_assignment1entity");
                        query.ColumnSet.AddColumns("new_email");
                        EntityCollection result1 = service.RetrieveMultiple(query);
                        foreach (var a in result1.Entities)
                        {
                            int size = result1.Entities.Count;
                            if (a.Attributes["new_email"].ToString().Equals(entity["new_email"]))
                                throw new InvalidPluginExecutionException("Duplicate Email found!");
                        }
                    }
                    catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
                    {
                        //You can handle an exception here or pass it back to the calling method.
                        throw new InvalidPluginExecutionException("Some problem occurred while Querying Records!");
                    }
                }
            }
        }
    }
}
公共类1:IPlugin
{
public void Execute(IServiceProvider服务提供程序)
{
//从服务提供程序获取执行上下文。
Microsoft.Xrm.Sdk.IPluginExecutionContext上下文=(Microsoft.Xrm.Sdk.IPluginExecutionContext)
GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
//获取对组织服务的引用。
组织服务工厂=
(IOOrganizationServiceFactory)服务提供者.GetService(类型为(IOOrganizationServiceFactory));
IOOrganizationService=factory.CreateOrganizationService(context.UserId);
//InputParameters集合包含消息请求中传递的所有数据。
if(context.InputParameters.Contains(“目标”)&&
context.InputParameters[“Target”]是实体)
{
实体=(实体)上下文。输入参数[“目标”];
//
//验证目标实体是否表示帐户。
//否则,此插件未正确注册。
if(context.MessageName.ToUpper()=“创建”)
{
if(entity.LogicalName==“新的\u分配1实体”)
{
尝试
{
QueryExpression query=newqueryexpression(“new_assignment1entity”);
query.ColumnSet.AddColumns(“新建电子邮件”);
EntityCollection result1=service.RetrieveMultiple(查询);
foreach(result1.Entities中的变量a)
{
int size=result1.Entities.Count;
if(a.Attributes[“new_email”].ToString().Equals(entity[“new_email”]))
抛出新的InvalidPlugineExecutionException(“找到重复的电子邮件!”);
}
}
捕获(错误异常)
{
//您可以在此处处理异常或将其传递回调用方法。
抛出新的InvalidPlugineExecutionException(“查询记录时出现了一些问题!”);
}
}
}
else if(context.MessageName.ToUpper()=“更新”)
{
if(entity.LogicalName==“新的\u分配1实体”)
{
尝试
{
QueryExpression query=newqueryexpression(“new_assignment1entity”);
query.ColumnSet.AddColumns(“新建电子邮件”);
EntityCollection result1=service.RetrieveMultiple(查询);
foreach(result1.Entities中的变量a)
{
int size=result1.Entities.Count;
if(a.Attributes[“new_email”].ToString().Equals(entity[“new_email”]))
抛出新的InvalidPlugineExecutionException(“找到重复的电子邮件!”);
}
}
捕获(错误异常)
{
//您可以在此处处理异常或将其传递回调用方法。
抛出新的InvalidPlugineExecutionException(“查询记录时出现了一些问题!”);
}
}
}
}
}
}
当用户创建具有重复电子邮件地址的新实体记录时,此代码起作用,并显示一个打印错误消息的对话框。但是,当用户编辑现有记录(更新和现有记录)并使电子邮件地址重复时,此代码将不起作用,并保存了重复电子邮件地址的更新记录。
我猜带有UPDATE else部分的上下文消息不起作用。

请帮帮我。

不值得尝试调试,因为不幸的是,您正在以一种极其低效的方式进行调试。(尽管最可能的原因是您查询的方式受到CRM“功能”的约束,这意味着您没有查询您认为是的所有记录)

简而言之,您的代码表示:

  • 获取
    new\u assignment1entity
    实体的所有(*)实例
  • 查看每个记录,直到我找到一个电子邮件地址与更新中提供的值匹配(区分大小写)
  • 遇到第一个完全匹配时引发异常(否则继续事务)
主要注意事项:

  • QueryExpression只返回CRM中最多前5000条记录
  • 您应该对查询进行筛选,以仅返回
    new\u assignment1entity
    记录,其中
    new\u email
    属性与提供的值匹配
  • String.Equals(String)
    区分大小写,因此要真正检查重复项,您应该转换每个值的大小写
  • 您的
    size
    变量没有任何作用
  • 如果新的/更新的记录没有
    new\u email
    的值,您的代码将抛出异常。在尝试访问该属性之前,应该检查该属性是否存在

  • 我解决了这个问题。为什么只运行创建执行流而不更新的问题是,我只为创建消息步骤注册了插件。为了解决这个问题,我在同一个插件中添加了一个新步骤,并用更新消息注册了它,如下面的屏幕截图所示:

    而且它很有魅力


    除此之外,@GregOwens还提到了非常有用的几点。这些都应该作为CRM开发的最佳实践来遵循。

    这是一个疯狂的问题,但你试过了吗