//The relationship schema
string relationshipName = "new_account_new_contact";
//Create a query that will check to see if the relationship already exists between contacts related to the Account
QueryExpression query1 = new QueryExpression(relationshipName)
{
NoLock = true,
ColumnSet = new ColumnSet(true),//only get the row ID, since we don't need any actual values
Criteria =
{
Filters =
{
new FilterExpression
{
// FilterOperator = LogicalOperator.And,
Conditions =
{
//Get the row for the relationship where the account and contact are the account
new ConditionExpression("accountid", ConditionOperator.Equal, entity.Id),
},
},
}
}
};
EntityCollection retrievedRelations = service.RetrieveMultiple(query1);
EntityReferenceCollection existingContactsList = new EntityReferenceCollection();
foreach (Entity retrievedRelation in retrievedRelations.Entities)
{
EntityReference existingContact = new EntityReference("contact", (Guid)retrievedRelation .Attributes["contactid"]);
existingContactsList.Add(existingContact );
}
service.Associate("account", newAccountId, new Relationship(relationshipName), existingContactsList);
string relationshipName = "new_account_new_contact";
//Create a query that will check to see if the relationship already exists between contacts related to the Account
QueryExpression query1 = new QueryExpression(relationshipName)
{
NoLock = true,
ColumnSet = new ColumnSet(true),//only get the row ID, since we don't need any actual values
Criteria =
{
Filters =
{
new FilterExpression
{
// FilterOperator = LogicalOperator.And,
Conditions =
{
//Get the row for the relationship where the account and contact are the account
new ConditionExpression("accountid", ConditionOperator.Equal, entity.Id),
},
},
}
}
};
EntityCollection retrievedRelations = service.RetrieveMultiple(query1);
EntityReferenceCollection existingContactsList = new EntityReferenceCollection();
foreach (Entity retrievedRelation in retrievedRelations.Entities)
{
EntityReference existingContact = new EntityReference("contact", (Guid)retrievedRelation .Attributes["contactid"]);
existingContactsList.Add(existingContact );
}
service.Associate("account", newAccountId, new Relationship(relationshipName), existingContactsList);
private static bool RelationshipExists(IOrganizationService service,
ReplyDeletestring relationshipname, Guid entity1Id, string entity1Name,
Guid entity2Id, string entity2Name)
{
string relationship1EtityName = string.Format("{0}id", entity1Name);
string relationship2EntityName = string.Format("{0}id", entity2Name);
//This check is added for self-referenced relationships
if (entity1Name.Equals(entity2Name, StringComparison.InvariantCultureIgnoreCase))
{
relationship1EtityName = string.Format("{0}idone", entity1Name);
relationship1EtityName = string.Format("{0}idtwo", entity1Name);
}
QueryExpression query = new QueryExpression(entity1Name)
{
ColumnSet = new ColumnSet(false)
};
LinkEntity link = query.AddLink(relationshipname,
string.Format("{0}id", entity1Name), relationship1EtityName);
link.LinkCriteria.AddCondition(relationship1EtityName,
ConditionOperator.Equal, new object[] { entity1Id });
link.LinkCriteria.AddCondition(relationship2EntityName,
ConditionOperator.Equal, new object[] { entity2Id });
return service.RetrieveMultiple(query).Entities.Count != 0;
}