web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to check if Contacts is associated with any other entities ?

(0) ShareShare
ReportReport
Posted on by

Hi Guys,

Is it possible to check if the contact is associated with any other entities. I have some to 1000 contacts to check if it is associated with any other contacts or not. Please guide through this.

Thanks,

Vignesh M

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: How to check if Contacts is associated with any other entities ?

    You need to check the relationships between contacts and all related entities, and get a count of the records.

    If any relationship has a contact of records that is > 0, that particular contact has a relationship.

    Retrieve Relationships:

                   RetrieveEntityRequest request = new RetrieveEntityRequest()

                   {

                       LogicalName = "contact",

                       EntityFilters = EntityFilters.Relationships,

                       RetrieveAsIfPublished = true

                   };

                   RetrieveEntityResponse response = (RetrieveEntityResponse)service.Execute(request);

    OneToManyRelationshipMetadata oneToNRelationship = response.EntityMetadata.OneToManyRelationships;

    Retrieve Child Record Count:

                   OneToManyRelationshipMetadata[] oneToNRelationships = response.EntityMetadata.OneToManyRelationships;

                   foreach (OneToManyRelationshipMetadata rel in oneToNRelationships)

                   {

                       string childEntityName = rel.ReferencingEntity;

                       string childEntityField = rel.ReferencingAttribute;

                       RetrieveEntityCount(childEntityName, childEntityField);

                   }

    Hope this helps.

  • Verified answer
    Community Member Profile Picture
    on at
    RE: How to check if Contacts is associated with any other entities ?

    Hi Aric,

    Thanks for the code, but I want to check for the contacts read from excel sheet. Is it possible to filter based on contact id or "custom attribute" in RetrieveEntityRequest.

    Thanks,

    Vignesh

  • Verified answer
    David Jennaway Profile Picture
    14,065 on at
    RE: How to check if Contacts is associated with any other entities ?

    The above code is to read the metadata - i.e. just to identify the relationships that a contact can have. You'll then need to use this information to check each contact.

    I don't think it's practical to use just one query to check all relationships, so I think your overall flow will need to be:

    • Use whatever makes sense to get a list of the contacts to check. If they are in CRM, I'd use a QueryExpression or Fetch with the filtering, but you mention an Excel sheet, so maybe you want to read this list from there
    • Loop through each contact, then query each relationship in turn to check if there is related data

    This may not seem hugely efficient for processing, but you should get reasonable enough performance. To improve performance you could query for each relationship in turn, linked to your list of contacts, then process the results, but the coding is probably more complex

  • Community Member Profile Picture
    on at
    RE: How to check if Contacts is associated with any other entities ?

    Yea David, I agree with you. I am struck with how to pass the contactId in to the above code.

  • Verified answer
    Shahbaaz Ansari Profile Picture
    6,211 on at
    RE: How to check if Contacts is associated with any other entities ?

    Hi Mohan,

    Try like below

    Retrieve Relationships:

                  RetrieveEntityRequest request = new RetrieveEntityRequest()

                  {

                      LogicalName = "contact",

              Id = "GUID"//pass your contact guid here

                      EntityFilters = EntityFilters.Relationships,

                      RetrieveAsIfPublished = true

                  };

    Thanks,

    Shahbaaz

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans