jueves, 12 de septiembre de 2013

CRM 4.0 - Relacionar entidades Muchos a Muchos (M:M) (sacado de otro blog)

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);

No hay comentarios:

Publicar un comentario