【问题标题】:Can I do an UpsertRequest that checks if the name of the entity exists?我可以执行 UpsertRequest 检查实体名称是否存在吗?
【发布时间】:2017-11-10 14:21:23
【问题描述】:

在 C# 中,我正在编写一个程序,将数据从旧的 Microsoft Dynamics CRM 系统传输到新系统。

对于大多数实体,我可以使用 UpsertRequest。但是,对于联系人和客户,新环境中已经有记录。由于我不想有双打,我想让 UpsertRequest 检查帐户的“姓名”字段和联系人的“全名”。

这可能吗?我(搜索了很多并且)找不到这方面的例子。如果没有,最好的方法是什么?

感谢您的任何反馈。

【问题讨论】:

    标签: c# microsoft-dynamics upsert


    【解决方案1】:

    对于这种情况,我会在创建联系人和帐户的消息时制作一个插件,例如:

    public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService =
               (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));
    
            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService organizationService = serviceFactory.CreateOrganizationService(context.UserId);
    
            Entity TargetEntity = new Entity();
    
    
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                TargetEntity = (Entity)context.InputParameters["Target"];
    
    
                QueryExpression queryDuplicateDetect = new QueryExpression(TargetEntity.LogicalName);
    
                if (TargetEntity.LogicalName == "account" && TargetEntity.Attributes.Contains("name"))
                {
                    queryDuplicateDetect.ColumnSet = new ColumnSet(new string[] { "name" });
                    queryDuplicateDetect.Criteria.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, TargetEntity["name"].ToString()));
                }
                else if (TargetEntity.LogicalName == "contact" && TargetEntity.Attributes.Contains("fullname"))
                {
                    queryDuplicateDetect.ColumnSet = new ColumnSet(new string[] { "fullname" });
                    queryDuplicateDetect.Criteria.AddCondition(new ConditionExpression("fullname", ConditionOperator.Equal, TargetEntity["fullname"].ToString()));
                }
    
                try
                {
                    EntityCollection resultsColl = organizationService.RetrieveMultiple(queryDuplicateDetect);
                    if (resultsColl.Entities.Count > 0)
                    {
                        foreach (Entity e in resultsColl.Entities)
                        {
                            tracingService.Trace("Record Found with ID {0}", e.Id);
                        }
    
                        //log results in some entity for more info 
                        throw new InvalidPluginExecutionException("Duplicate detected.");
    
                    }
                }
                catch (Exception e)
                {
                    throw new InvalidPluginExecutionException(e.Message);
                }
            }
        }
    

    在创建方面,我将使用简单的 try catch 来跳过现有记录

     Entity x = new Entity("account");
                x["name"] = "mohamed0";
    
                Entity y = new Entity("contact");
                y["fullname"] = "mohamed";
    
                Entity z = new Entity("contact");
                z["fullname"] = "mohamed";
    
    
                try
                {
    
    
                    var r = _orgService.Create(x);
                    r = _orgService.Create(y);
                    r = _orgService.Create(z);
                }
                catch (Exception e)
                {
    
                    throw;
                }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多