2011-12-21 4 views
1

(いや、ないフランス人の女の子再び!!!)取得のn

だから... ...私はN-Nの関係にある2つのエンティティがあります: "new_produit" と "new_lignecontratを"。 最後に "new_lignecontrat"レコードの "new_produit"レコードを新しく作成した "new_lignecontrat"にコピーする必要があります。

プラグインは、new_lignecontratの作成時にトリガーされます。

これまでのところ、私はこれを書いたが、私は「new_produit」レコードをコピーするために従うべき手順の試合はかなりわからないんだけど...

  else if (modeleContrat.Id.Equals (ContratForfaitaire)) 
      { 

       FetchExpression fetch = new FetchExpression(@" 
        <fetch distinct='false' mapping='logical'> 
         <entity name='new_contrats'><link-entity name='" + context.PrimaryEntityName + "' 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); 

       if (lines.Entities.Any()) 
       { 
        var last = lines.Entities.Last(); 

        if (last.GetAttributeValue<OptionSetValue>("statecode").Value == 1) 
        { 

         QueryExpression query = new QueryExpression(); 
         query.EntityName = "new_produit"; 
         query.ColumnSet = new ColumnSet("new_produitid"); 
         Relationship relationship = new Relationship(); 

         // name of relationship between team & systemuser 
         relationship.SchemaName = "new_new_lignecontrat_new_produit"; 
         RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection(); 
         relatedEntity.Add(relationship, query); 
         RetrieveRequest request = new RetrieveRequest(); 
         request.RelatedEntitiesQuery = relatedEntity; 
         request.ColumnSet = new ColumnSet("new_lignecontratid"); 
         request.Target = new EntityReference 
         { 
          Id = last.Id, 
          LogicalName = last.LogicalName 

         }; 

         RetrieveResponse response = (RetrieveResponse)service.Execute(request); 



         if (((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities)))).Contains(new Relationship("new_new_lignecontrat_new_produit")) && ((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities))))[new Relationship("new_new_lignecontrat_new_produit")].Entities.Count > 0) 
         { 
          response.Entity.Attributes.Remove("new_produitid"); 
          response["new_lignecontratid"] = new EntityReference(target.LogicalName, target.Id); 
+3

;) –

+0

Oooops ...私は完全に答えを受け入れることを忘れてしまった...申し訳ありません><」 – MademoiselleLenore

+2

あなたは以来、謝罪する必要はありません。あなたは問題を修正しました:) –

答えて

3

私が反応して、部分的にこの質問に答え信じますone of your previous questionsに、私の答えが今回より良いかどうかを見てみましょう。

この問題では三つのステップ、私は問題を正しく理解していれば、最初の最後new_lignecontratに関連するすべてのnew_produitレコードを取得し、その後、現在のnew_lignecontratレコードの作成前に作成された最後のnew_lignecontratレコードを取得することで、最終的にAssociatenew_lignecontratレコードのそれぞれnew_produitレコードです。

  1. このクエリでは、Dynamics(FetchXml、QueryExpression、Linq)でサポートされている3つのクエリメソッドのいずれかを使用できます。私は.First()メソッドが効率的なTOPクエリ(最後に作成されたnew_lignecontratレコードを取得する)を実行する簡単な方法だからLinqを好んでいますが、FetchXmlとページングCookieを使用して同じ(クエリー式についてはわかりません)を達成することもできます。あなたはNを使用してリンクnew_produitレコードを取得することができます

  2. :Nテーブルと新しいnew_lignecontratのIDと最後に関連付けられたnew_produitたレコードのIDを持っていたら、手順1

  3. で取得したIDを、これらのレコードとその関係に対してIOrganizationServiceAssociateメソッドを実行します。以下は、これらの3つのステップを処理するモックアップです。フランス人の女の子であることは答えを受け付けないように、言い訳にはならない

using (OrganizationServiceContext osc = new OrganizationServiceContext(service)) 
{ 
    //assumes early binding, but this can be modified for late binding as well 
    Guid ligneContratID = (from lc in osc.CreateQuery<new_ligneContrat>() 
          where lc.CreatedOn < (DateTime)targetEntity.Attributes["CreatedOn"] //look at all new_ligneContrat records created before the newly created record 
          orderby lc.CreatedOn descending //sort newest to oldest 
          select lc.new_ligneContratID) 
          .First(); //get the newest record 

    var produits = from lcp in osc.CreateQuery<new_new_lignecontrat_new_produit>() //use your N:N relationship/table as your linking table 
        join p in osc.CreateQuery<new_produit>() on lcp.new_produitId equals p.new_produitId 
        where lcp.new_lignecontratId = ligneContratID //use the previous query result in your N:N lookup 
        select p.new_produitId;       

    EntityReferenceCollection erc = new EntityReferenceCollection(); 
    foreach (var e in produits) 
    { 
     erc.Add(new EntityReference("new_produit", e)); 
    } 

    service.Associate("new_lignecontrat", targetEntity.Id, new Relationship("new_new_lignecontrat_new_produit", erc); 
} 
+0

こんにちはピーターとありがとう。あなたの前の答えは悪くないし、とても役に立ちました。私はqueryexpressionsが私たちが望んでいたものを達成するための最良の方法だと言われましたが、私はLinqメソッドをコーディングするほうが賢明です。 – MademoiselleLenore

関連する問題