次の方法で共有


エンティティ クラスを使用して関連レコード間の関連付けを追加または更新する

 

公開日: 2016年11月

対象: Dynamics CRM 2015

Microsoft Dynamics CRM 2015 および Microsoft Dynamics CRM Online 2015 更新プログラム では、IOrganizationService を使用することができます。Associate および IOrganizationService。関連レコード間の関連付けを作成および削除するための Disassociate メソッド。

関連付けを作成するには、まず関連付けの対象となるターゲット エンティティの一意の ID を決めます。 その後、ターゲット エンティティと関連付けるエンティティのコレクションを作成します。 次に、コレクション内のエンティティとターゲット エンティティ間の関係を定義します。 最後に、この情報を Associate メソッドに渡します。 関連付けを削除するときは、同じ情報を Disassociate メソッドに渡します。

次のコード例は、関連するレコード間の関連付けの作成方法と、関連付けの解除方法を示しています。

// The account ID would typically be passed in as an argument or determined by a query.
// The contact ID would typically be passed in as an argument or determined by a query.
// Associate the accounts to the contact record. 
//Create a collection of the entity ids that will be associated to the contact.
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("account", _account1Id));
relatedEntities.Add(new EntityReference("account", _account2Id));
relatedEntities.Add(new EntityReference("account", _account3Id)); 
// Create an object that defines the relationship between the contact and account.
Relationship relationship = new Relationship("account_primary_contact");
  //Associate the contact with the 3 accounts.
_orgService.Associate("contact", _contactId, relationship, relatedEntities); 
Console.WriteLine("The entities have been associated."); 
//Disassociate the records.
_orgService.Disassociate("contact", _contactId, relationship, relatedEntities); 
Console.WriteLine("The entities have been disassociated.");

関連項目

コードでの遅延バインドされたエンティティ クラスの使用
作成、更新、および削除のエンティティ クラスの使用
エンティティ関係の動作
サンプル: 作成、取得、更新、および削除 (遅延バインド)

© 2017 Microsoft. All rights reserved. 著作権