如何检查关系是否已经存在

本文关键字:是否 存在 关系 何检查 检查 | 更新日期: 2023-09-27 18:02:34

在我的CRM中,我有一个与具有N:N关系的"Contact"实体相关的"Advisor"实体。创建Advisor实体之后,我用以下代码将Advisor与Contact关联:

_service.Associate("contact", contactid, relationship, relatedAdvisors);

relatedAdvisors是一个实体引用集合。但是,如果关系已经存在,则会得到错误

"Cannot insert duplicate Key"

我如何检查这个顾问是否已经与联系人相关?

如何检查关系是否已经存在

下面的查询应该适合您。然而,我没有编译它,但你可以得到一个想法。

// Create new object of RelatedAdvisors
var newRelatedAdvisors = new new_RelatedAdvisors();
// Go through all the advisors in colletion
foreach(var advisor in relatedAdvisors)
{
    // Search of existing relationship between contact and advisor
    var existingAdvisor = (from a in linqContext.CreateQuery<new_RelatedAdvisors>() where a.contactid ==   contactid && a.Id == advisor.Id select a).FirstOrDefault();
    // if relationship does not exist then add add advisor to newly created collection object
    if(xyz == null || xyz.Count() == 0)
    {
        // Add advisor to newRelatedAdvisors 
    }
}
// associate the contact with newly created collection of relatedadvisors
_service.Associate("contact", contactid, relationship, newRelatedAdvisors);

注意

我没有编译这段代码,因为目前我没有访问开发系统。但它可能会帮助你得到一个想法。

希望对您有所帮助:

private static bool RelationshipExists(IOrganizationService service, string 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;
}

基于@Saddam Khan提供的代码,这是我想到的。这允许从relatedEntities集合中排除已经关联的实体,并且总是将任意数量的实体与两个请求关联/解除关联(首先检查它们是否已经被暗杀,然后关联那些没有被暗杀的实体)。

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
public static class OrganizationServiceExtension
{
    public static void AssociateIfDisassociated(this IOrganizationService service, EntityReference entity, string relationshipName, string relationshipEntityName, EntityReferenceCollection relatedEntities)
    {
        List<EntityReference> unrelatedEntities = relatedEntities.Except(service.RetrieveRelated(entity, relationshipEntityName, relatedEntities)).ToList();
        if (!unrelatedEntities.Any())
        {
            return;
        }
        service.Associate(entity.LogicalName, entity.Id, new Relationship(relationshipName), new EntityReferenceCollection(unrelatedEntities));
    }
    public static void DisassociateIfAssociated(this IOrganizationService service, EntityReference entity, string relationshipName, string relationshipEntityName, EntityReferenceCollection relatedEntities)
    {
        List<EntityReference> realRelatedEntities = relatedEntities.Intersect(service.RetrieveRelated(entity, relationshipEntityName, relatedEntities)).ToList();
        if (!realRelatedEntities.Any())
        {
            return;
        }
        service.Disassociate(entity.LogicalName, entity.Id, new Relationship(relationshipName), new EntityReferenceCollection(realRelatedEntities));
    }
    public static EntityReferenceCollection RetrieveRelated(this IOrganizationService service, EntityReference entity, string relationshipEntityName, EntityReferenceCollection checkIfRelatedEntities)
    {
        var relatedEntities = new EntityReferenceCollection();
        if (checkIfRelatedEntities?.Any() != true)
        {
            return relatedEntities;
        }
        string relatedEntityLogicalName = checkIfRelatedEntities.First().LogicalName;
        string entityIdFieldName = $"{entity.LogicalName}id";
        string relatedEntityIdFieldName = $"{relatedEntityLogicalName}id";
        //This check is added for self-referenced relationships
        if (entity.LogicalName.Equals(relatedEntityLogicalName, StringComparison.InvariantCultureIgnoreCase))
        {
            entityIdFieldName = $"{entity.LogicalName}idone";
            relatedEntityIdFieldName = $"{relatedEntityLogicalName}idtwo";
        }
        var relatedEntitiesQuery = new QueryExpression(relatedEntityLogicalName) 
        { 
            ColumnSet = new ColumnSet(relatedEntityIdFieldName),
            LinkEntities =
            {
                new LinkEntity
                {
                    LinkToEntityName = relationshipEntityName,
                    LinkFromAttributeName = $"{relatedEntityLogicalName}id",
                    LinkToAttributeName = relatedEntityIdFieldName,
                    LinkCriteria =
                    {
                        Conditions = 
                        { 
                            new ConditionExpression(entityIdFieldName, ConditionOperator.Equal, entity.Id),
                            new ConditionExpression(relatedEntityIdFieldName, ConditionOperator.In, checkIfRelatedEntities.Select(e => e.Id).ToArray())
                        }
                    }
                }
            }
        };
        List<EntityReference> relatedEntitiesList = service.RetrieveMultiple(relatedEntitiesQuery).Entities
            .Select(e => e.ToEntityReference())
            .ToList();
        relatedEntities.AddRange(relatedEntitiesList);
            
        return relatedEntities;
    }
}

System UserSecurity RoleSystem UserTeam示例用法:

var user = new EntityReference(SystemUser.EntityLogicalName, Guid.NewGuid());
service.AssociateIfDisassociated(
    user,
    "systemuserroles_association",
    SystemUserRoles.EntityLogicalName,
    new EntityReferenceCollection { new EntityReference(Role.EntityLogicalName, Guid.NewGuid()) });
service.AssociateIfDisassociated(
    user,
    "teammembership_association",
    TeamMembership.EntityLogicalName,
    new EntityReferenceCollection { new EntityReference(Team.EntityLogicalName, Guid.NewGuid()) });