.NET 用 SDK を使用したテーブル行の関連付けと関連付け解除

テーブル行は、関連するテーブル行のルックアップ列を使用して相互に関連付けられます。 一対多の関係にある 2 つの行を関連付ける最も簡単な方法は、EntityReference を使用して、関連する行のルックアップ列の値を設定することです。

一対多の関係にある 2 つの行の関連付けを解除する最も簡単な方法は、ルックアップ列の値を null に設定することです。

多対多の関係を使用する関連付けも、多対多の関係をサポートする 交差エンティティ のルックアップ列に依存します。 これらの関係は、その交差エンティティの行の存在によって定義されます。 交差エンティティを直接操作することはできますが、API を使用する方がはるかに簡単です。

Associate メソッドまたは AssociateRequest の使用

IOrganizationService.Associate メソッド、または または AssociateRequestIOrganizationService.Execute メソッド で使用する際の主な価値は、次のことができることです。

  • 1 回の操作で複数の行を関連付ける
  • 交差エンティティを意識することなく、多対多の関係を使用して行を簡単に関連付ける。

テーブル行をこれらの API に関連付けるには、次の 3 つが必要です:

  • 関連付ける行へのエンティティ参照
  • 関連付けの名前
  • テーブル行を関連付ける 1 つ以上の参照

関係が 1 対多であるか多対多であるかは関係ありません。 パラメータまたはプロパティは同等です。

メタデータ ブラウザを使用してカスタマイズ UI またはメタデータを表示することによって、関係の名前を見つけることができます。

詳細:

次の例では、Redmond にあるすべてのアカウントの取引先責任者として特定の連絡先 (jimGlynn) を設定します。


// Retrieve the accounts
var query = new QueryByAttribute("account")
{
ColumnSet = new ColumnSet("name")
};
query.AddAttributeValue("address1_city", "Redmond");

EntityCollection accounts = svc.RetrieveMultiple(query);

//Convert the EntityCollection to a EntityReferenceCollection
var accountReferences = new EntityReferenceCollection();

accounts.Entities.ToList().ForEach(x => {
accountReferences.Add(x.ToEntityReference());
});

// The contact to associate to the accounts
var jimGlynn = new EntityReference("contact", 
new Guid("cf76763a-ba1c-e811-a954-000d3af451d6"));

// The relationship to use
var relationship = new Relationship("account_primary_contact");

// Use the Associate method
svc.Associate(jimGlynn.LogicalName, jimGlynn.Id, relationship, accountReferences);

それによって特別な利点はありませんが、AssociateRequest を使用する場合は、最後の行を次のように置き換えることができます。

// Use AssociateRequest
AssociateRequest request = new AssociateRequest()
{
RelatedEntities = accountReferences,
Relationship = relationship,
Target = jimGlynn
};

svc.Execute(request);

この操作は、Account.PrimaryContactId ルックアップ列に対する 3 つの個別の更新操作と同じですが、取引先企業に対する多対一のエンティティの関係と、取引先担当者に対する一対多のエンティティの関係である account_primary_contact の関連付けを使用しています。

関係列のプロパティを調べると、ReferencingEntity 値が account で、ReferencingAttribute 値が primarycontactid であることがわかります。

Disassociate メソッドまたは DisassociateRequest の使用

IOrganizationService.Disassociate メソッドまたは DisassociateRequestIOrganizationService.Execute メソッドは メソッドは、テーブル行を関連付ける方法の逆です。

次のコードは、上記のサンプルで作成された関連付けを元に戻します。

// Retrieve the accounts
var query = new QueryByAttribute("account")
{
ColumnSet = new ColumnSet("name")
};
query.AddAttributeValue("address1_city", "Redmond");

EntityCollection accounts = svc.RetrieveMultiple(query);

//Convert the EntityCollection to a EntityReferenceCollection
var accountReferences = new EntityReferenceCollection();

accounts.Entities.ToList().ForEach(x => {
accountReferences.Add(x.ToEntityReference());
});

// The contact to associate to the accounts
var jimGlynn = new EntityReference("contact", 
new Guid("cf76763a-ba1c-e811-a954-000d3af451d6"));

// The relationship to use
var relationship = new Relationship("account_primary_contact");

// Use the Disassociate method
svc.Disassociate(jimGlynn.LogicalName, jimGlynn.Id, relationship, accountReferences);

それによって特別な利点はありませんが、DisassociateRequest を使用する場合は、最後の行を次のように置き換えることができます。

// Use DisassociateRequest
DisassociateRequest request = new DisassociateRequest()
{
RelatedEntities = accountReferences,
Relationship = relationship,
Target = jimGlynn
};

svc.Execute(request);

参照

.NET 用 SDK を使用したテーブル行の作成
SDK for .NET を使用してテーブルの行を取得する
SDK for .NET を使用したテーブル行の更新と削除

注意

ドキュメントの言語設定についてお聞かせください。 簡単な調査を行います。 (この調査は英語です)

この調査には約 7 分かかります。 個人データは収集されません (プライバシー ステートメント)。