2017-11-10 13 views
0

C#에서는 이전 Microsoft Dynamics CRM 시스템의 데이터를 새 시스템으로 전송하는 프로그램을 작성했습니다.엔티티의 이름이 존재하는지 확인하는 UpsertRequest를 수행 할 수 있습니까?

대부분의 엔티티에서 UpsertRequest를 사용할 수 있습니다. 연락처와 계정의 경우 새 환경에 이미 레코드가 있습니다. 복식을 원하지 않기 때문에 계정의 경우 "이름"필드를, 연락처의 경우 "성명"필드에서 UpsertRequest 검사를하고 싶습니다.

그럴 수 있습니까? 나 (많이 검색 한)이 예제를 찾을 수 없습니다. 그렇지 않다면 앞으로 나아갈 수있는 가장 좋은 방법은 무엇입니까?

의견을 보내 주셔서 감사합니다. 나는 메시지에 플러그인을 만들 것 같은 경우에

답변

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); 
      } 
     } 
    } 

내가 기존 레코드를 건너 간단한 시도 캐치를 사용할 것이다 측면을 만들에

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; 
      } 

희망이 도움이 될 것입니다.