2012-02-03 1 views
2

엔티티의 관련 레코드를 새로 생성 된 레코드에 연결하려고합니다. 플러그인은 생성 및 사전 작업시 트리거됩니다.어소시에이트 메서드

새로운 개체로 컬렉션을 연결하려고 할 때 오류가 발생합니다

public void Execute(IServiceProvider serviceProvider) 
     { 
      // Instanciation des services 

      IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 
      IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
      IOrganizationService service = factory.CreateOrganizationService(null); 


      Entity target = (Entity)context.InputParameters["Target"]; 
      EntityReference contrats = (EntityReference)target.Attributes["new_contratsid"]; 

      FetchExpression fetch = new FetchExpression(@" 
        <fetch distinct='false' mapping='logical'> 
         <entity name='" + context.PrimaryEntityName + "'><link-entity name='new_contrats' alias='nombreligne' from='new_contratsid' to='new_contratsid'><filter type='and'><condition attribute='new_contratsid' value='" + contrats.Id + "' operator='eq'></condition></filter></link-entity></entity></fetch>"); 

      EntityCollection lines = service.RetrieveMultiple(fetch); 

        // Vérification qu'il y a au moins une ligne de contrat associée 
      if (lines.Entities.Any()) 
      { 

       var first = lines.Entities.Last(); 

       if (first.GetAttributeValue<OptionSetValue>("statecode").Value == 1) 
       { 
        FetchExpression query = new FetchExpression(@" 
         <fetch distinct='false' mapping='logical'> 
         <entity name='incident'><filter type='and'><condition attribute='new_lignecontrat' value='"+first.Id+"' operator='eq'/></filter></entity></fetch>"); 

        EntityCollection incident = service.RetrieveMultiple(query); 

        if (incident.Entities.Any()) 
        { 


         foreach (var e in incident.Entities) 
         { 
          e.Attributes["new_lignecontrat"] = new EntityReference (target.LogicalName, target.Id); 
         } 


       } 
       } 
      } 
: 여기

내 코드입니다 "존재하지 않는 아이디 = ad630ba6-684e-e111-92e3-00155d151905와 new_ligneContrat을"

무엇이 잘못 되었나요 ???

미리 감사드립니다.

편집 1 : ok는 아직 레코드가 존재하지 않으므로 논리적으로 보임> <. 한 가지만 : 조회 필드의 값을 어떻게 변경할 수 있습니까? 그 유형은 무엇입니까?

편집 2 : 코드를 실행할 때 오류가 발생하지 않지만 인시던트 엔티티 필드가 업데이트되지 않음> < '... 내 코드를 invalidPluginExceptions로 테스트했는데 코드 끝에 도달했습니다. 코드 업데이트 ...

답변

3

원래 질문의 편집에 대답하기 위해, 그래, 당신은 다른 레코드 핵심 데이터베이스 작업과 기록을 연결할 수 없습니다 : 3

편집 : ... 여기에 코드입니다 아직 끝나지 않았다.

Pre-operation : 단계의 주요 시스템 작업 전에 실행할 수 있습니다 플러그인을위한 파이프 라인이다. 이 단계에서 등록 된 플러그인은 데이터베이스 트랜잭션 내에서 으로 실행됩니다.

그래서 당신은 포스트 동작으로 무대를 변경하거나 하나 개 IPlugin 클래스는 사전 작업 단계를 처리하고 또 다른 하나 또는 여러 프로젝트에서 수술 후 단계를 처리 할 수 ​​있습니다 중 하나, 연결을 처리한다.

편집에 응답하려면 조회 필드의 클래스는 EntityReference입니다. (1 : N 관계로 작업하는 것처럼 보입니다.)

두 번째 편집에 대한 답변을 보려면 Entity에 새로운 EntityReference을 할당하는 코드 스 니펫이 어디에도 보이지 않습니다. 또한 핵심 데이터베이스 작업이 아직 수행되지 않았기 때문에 사전 작업 단계에서 서비스에 Update 요청을 보낼 필요가 없습니다. Entity의 속성을 선택한 값과 동일하게 설정하면이 변경 사항이 데이터베이스로 옮겨집니다.

if (entity.Attributes.ContainsKey("new_lignecontrat")) 
{ 
    entity.Attributes["new_lignecontrat"] = YourEntityReference; 
} 
else //attribute not included in the plugin operation 
{ 
    entity.Attributes.Add("new_lignecontrat", YourEntityReference); 
} 

Microsoft는 SDK에서이 개념의 데모를 가지고 :

\ SDK \에 SampleCode \ CS \ 플러그인 \

+0

accountnumberplugin.cs 투어 답변 피터 주셔서 감사합니다! 포스트 오페라에서 플러그인을 시작하는 것은 문제가되지 않습니다. 나는 실제로 1 : N 관계를 연구하고 있습니다. 실제로 필요한 것은 새로운 "new_lignecontrat"가 생성되면 인시던트 엔티티에서 이전 엔트리와 관련된 조회 필드가 업데이트됩니다. 그래서 간단합니다. e.Attributes.getValue ("new_lignecontrat") = 새로운 EntityReference (target.logicalName, target.Id); 그 일을 해? – MademoiselleLenore

+0

@MademoiselleLenore : 맞습니다.N : N 관계에 대해서는'Associate' 메소드를 사용하지만, 1 : N 관계에 대해서는 엔티티에'EntityReference'를 직접 할당 할 수있는 사치가 있습니다. 그러면 잘 작동합니다. –

+0

감사합니다 피터, 그냥 내 게시물을 편집 한 이유가 작동하지 않습니다 ... – MademoiselleLenore

관련 문제