Hi,
To find out whether a relationship exists between two entities or not you can use the following code:
For 1 to N:
To find out whether a relationship exists between two entities or not you can use the following code:
For 1 to N:
public bool Is1toNRelationshipExists(IOrganizationService service, string parent, string child)
{
RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.Relationships,
LogicalName = parent
};
RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveEntityRequest);
foreach (OneToManyRelationshipMetadata rel in retrieveEntityResponse.EntityMetadata.OneToManyRelationships)
{
if (rel.ReferencingAttribute == child)
{
return true;
}
}
return false;
}
For N to N:
public bool IsNtoNRelationshipExists(IOrganizationService service, string entity1, string entity2)
{
RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.Relationships,
LogicalName = entity1
};
RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveEntityRequest);
foreach (ManyToManyRelationshipMetadata rel in retrieveEntityResponse.EntityMetadata.ManyToManyRelationships)
{
if (rel.Entity2LogicalName == entity2)
{
return true;
}
}
return false;
}
Hope it helps..!! 🙂