我可以执行UpsertRequest来检查实体的名称是否存在吗?

时间:2017-11-10 14:21:24

标签: c# microsoft-dynamics upsert

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

对于大多数实体,我可以使用UpsertRequest。但是,对于联系人和帐户,新环境中已有记录。因为我不想有双打,所以我希望在帐户的情况下在“名称”字段上检查UpsertRequest,在联系人的情况下在“全名”上检查。

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

感谢您的反馈。

1 个答案:

答案 0 :(得分:0)

对于这种情况,我会在消息创建联系人和帐户上创建一个插件,如下所示:

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;
            }
希望这会有所帮助。