我需要在Lead和自定义实体之间以及Contact和自定义实体之间创建/定义多对多关系。我似乎找不到任何我想做的代码示例。
这需要在CRM 4和CRM 5中都有效。
做两个N:1关系而不是N:N关系有什么缺点吗?
答案 0 :(得分:0)
通过转到实体N:N关系,您可以通过UI执行N:N关系。与中间实体进行2 N:1关系比使用单个N:N更有优势,例如您可以存储关系属性(关系的角色),还可以通过双N:1关系链接工作流。
编辑:
要通过代码创建N:N关系,您可以参考下面摘录的CRM 2011 SDK帮助页面“创建和检索实体关系”。您可以通过4.0中的元数据服务 -
执行类似的操作创建N:N实体关系
以下示例使用EligibleCreateManyToManyRelationship方法验证Account和Campaign实体是否可以参与N:N实体关系,然后使用CreateManyToManyRequest创建实体关系。
bool accountEligibleParticipate =
EligibleCreateManyToManyRelationship("account");
bool campaignEligibleParticipate =
EligibleCreateManyToManyRelationship("campaign");
if (accountEligibleParticipate && campaignEligibleParticipate)
{
CreateManyToManyRequest createManyToManyRelationshipRequest =
new CreateManyToManyRequest
{
IntersectEntitySchemaName = "new_accounts_campaigns",
ManyToManyRelationship = new ManyToManyRelationshipMetadata
{
SchemaName = "new_accounts_campaigns",
Entity1LogicalName = "account",
Entity1AssociatedMenuConfiguration =
new AssociatedMenuConfiguration
{
Behavior = AssociatedMenuBehavior.UseLabel,
Group = AssociatedMenuGroup.Details,
Label = new Label("Account", 1033),
Order = 10000
},
Entity2LogicalName = "campaign",
Entity2AssociatedMenuConfiguration =
new AssociatedMenuConfiguration
{
Behavior = AssociatedMenuBehavior.UseLabel,
Group = AssociatedMenuGroup.Details,
Label = new Label("Campaign", 1033),
Order = 10000
}
}
};
CreateManyToManyResponse createManytoManyRelationshipResponse =
(CreateManyToManyResponse)_serviceProxy.Execute(
createManyToManyRelationshipRequest);
_manyToManyRelationshipId =
createManytoManyRelationshipResponse.ManyToManyRelationshipId;
_manyToManyRelationshipName =
createManyToManyRelationshipRequest.ManyToManyRelationship.SchemaName;
Console.WriteLine(
"The many-to-many relationship has been created between {0} and {1}.",
"account", "campaign");
}
<强> EligibleCreateManyToManyRelationship 强>
以下示例创建一个EligibleCreateManyToManyRelationship方法,该方法使用CanManyToManyRequest来验证实体是否可以参与N:N实体关系。
/// <summary>
/// Determines whether the entity can participate in a many-to-many relationship.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns></returns>
public bool EligibleCreateManyToManyRelationship(string entity)
{
CanManyToManyRequest canManyToManyRequest = new CanManyToManyRequest
{
EntityName = entity
};
CanManyToManyResponse canManyToManyResponse =
(CanManyToManyResponse)_serviceProxy.Execute(canManyToManyRequest);
if (!canManyToManyResponse.CanManyToMany)
{
Console.WriteLine(
"Entity {0} can't participate in a many-to-many relationship.",
entity);
}
return canManyToManyResponse.CanManyToMany;
}