|
public static void retrieveEntityListFromManyToMany(TitanMiscTests.CrmSdk.CrmService service)
{
//This code will retrieve a list of "leads" associated with the entity "Foo" on the relationship whose intersect entity is "new_account_lead_custom"
//Filter by the specific record that we are looking for
//(In this example we assume that there are no other accounts with the name Foo, otherwise
// if would be recommended to use the account "id" instead of the name.
ConditionExpression conditionName = new ConditionExpression();
conditionName.AttributeName = "name";
conditionName.Operator = ConditionOperator.Equal;
conditionName.Values = new object[1];
conditionName.Values[0] = "Foo";
FilterExpression selectByName = new FilterExpression();
selectByName.Conditions = new ConditionExpression[] { conditionName };
//Create nested link entity and apply filter criteria
LinkEntity nestedLinkEntity = new LinkEntity();
nestedLinkEntity.LinkToEntityName = "account";
nestedLinkEntity.LinkFromAttributeName = "accountid";
nestedLinkEntity.LinkToAttributeName = "accountid";
nestedLinkEntity.LinkCriteria = selectByName;
//Create the nested link entities
LinkEntity intersectEntity = new LinkEntity();
intersectEntity.LinkToEntityName = "new_account_lead_custom";
intersectEntity.LinkFromAttributeName = "leadid";
intersectEntity.LinkToAttributeName = "leadid";
intersectEntity.LinkEntities = new LinkEntity[] { nestedLinkEntity };
//Create Query expression and set the entity type to lead
QueryExpression expression = new QueryExpression();
expression.EntityName = "lead";
expression.LinkEntities = new LinkEntity[] { intersectEntity };
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = expression;
//Execute and examine the response
RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);
BusinessEntity[] entities=response.BusinessEntityCollection.BusinessEntities;
Console.WriteLine("Total related=" + entities.Length);
}
|