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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

query to retrieve all contacts with linked entity

(5) ShareShare
ReportReport
Posted on by 5,514
Hi all,
 
I have a requirement where client is asking us to give them  a query where we can retrieve.
 
All contacts who don't have account and no activities related to that account.
 
can i get both fetch xml query and SQL query.
 
 
 
Thanks
Sandeep
 
 
 
Categories:
I have the same question (0)
  • Layan Jwei Profile Picture
    8,097 Super User 2025 Season 2 on at
    Hi,
     
    Which D365 product are u talking about? Is it D365FO or CE?
  • Adam_Travers Profile Picture
    329 on at
    Hi, sorry I’m a bit confused, if you search for Contacts that do not have an Account then there is not need to add another condition for no Activities against the Account as the initial condition is to search for Contacts with no Account. 
  • sandeepc Profile Picture
    5,514 on at
    Hi @layan 
     
    Its D365 CE sales
  • sandeepc Profile Picture
    5,514 on at
    Hi @Adam 
     
    Client requirement is as below 
    No External Contacts (different to @ab.com) No account
    No Activities
    Write a query, to be run in PROD through XRMToolBox, 
    with the 3 conditions above to get the count and the email account of those contacts
  • Adam_Travers Profile Picture
    329 on at
    This should work but only if the Activity is associated to the Contact (Regarding set within the Activity) However, Im not sure what you are referring to with the 'No External Contacts (different to @ab.com)'  The below will return all Contacts with no Account and not related Activities

    SQL
     
    SELECT c.emailaddress1
    FROM Contact c
    LEFT JOIN ActivityPointer a ON c.ContactId = a.regardingobjectid
    WHERE c.parentcustomerid IS NULL AND c.ContactId NOT IN (SELECT a.RegardingObjectId FROM ActivityPointer a) 
     
    You can drop this SQL within XRM Toolbox to convert to XML.
  • sandeepc Profile Picture
    5,514 on at
    CCan we see in fetch XML query as we dont use SQL
     
    @adam travers
  • sandeepc Profile Picture
    5,514 on at
    Not all contacts  @adam
     
    Contacts who are not part of my company 
    Whose email address(primary email) doesn't contain @ab.com  those are called external contacts
  • Verified answer
    Adam_Travers Profile Picture
    329 on at
    Here you go Sandeep. I have rewritten it slightly different this time, this is only for the Activity types of Emails, Appointments, Tasks and Phone Calls.

    SQL:

    SELECT c.FullName, c.emailaddress1
    FROM Contact AS c
    LEFT JOIN ActivityParty AS ap ON c.ContactId = ap.PartyId
    LEFT JOIN ActivityPointer AS a ON ap.ActivityId = a.ActivityId
    WHERE (a.ActivityTypeCode NOT IN (4202, 4210, 4201, 4212) OR a.ActivityId IS NULL) AND c.parentcustomerid IS NULL AND c.emailaddress1 NOT LIKE '%ab.com%'
    GROUP BY c.FullName, c.emailaddress1
    HAVING COUNT(a.ActivityId) = 0


    Fetch XML:

    <fetch xmlns:generator="MarkMpn.SQL4CDS" aggregate="true">
      <entity name="contact">
        <attribute name="fullname" alias="fullname" groupby="true" />
        <attribute name="emailaddress1" alias="emailaddress1" groupby="true" />
        <link-entity name="activityparty" to="contactid" from="partyid" alias="ap" link-type="outer">
          <link-entity name="activitypointer" to="activityid" from="activityid" alias="a" link-type="outer">
            <attribute name="activityid" alias="a_activityid_count" aggregate="countcolumn" />
          </link-entity>
        </link-entity>
        <filter>
          <filter type="or">
            <condition attribute="activitytypecode" entityname="a" operator="not-in">
              <value>4202</value>
              <value>4210</value>
              <value>4201</value>
              <value>4212</value>
            </condition>
            <condition attribute="activityid" entityname="a" operator="null" />
          </filter>
          <condition attribute="parentcustomerid" operator="null" />
          <condition attribute="emailaddress1" operator="not-like" value="%ab.com%" />
          <condition attribute="emailaddress1" operator="not-null" />
        </filter>
        <order alias="fullname" />
        <order alias="emailaddress1" />
      </entity>
    </fetch>

    Please verify this answer if it has resolved your request.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans