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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM FetchXML Query to retrieve all Accounts that do not have active Contacts

(0) ShareShare
ReportReport
Posted on by

I am using CRM 2015 on prem. I have a windows scheduled task that triggers an exe app which the queries CRM and trigger an Action Workflow. Now on my exe app, I want to modify my fetchxml to retrieve only Accounts that  are due for renewal in the next 60 days and in the past 30 days. It should then filter further and return only the Accounts that do not have active Contacts. (An Account may have multiple contacts, if none of them are active, give me that Account for further processing). Frequency is time period for renewal e.g Monthly, Annually, etc...

Here is my FetchXML:

<fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="account">
<attribute name="accountid"/>
<attribute name="wib_name"/>
<attribute name="createdon"/>
<order descending="false" attribute="wib_name"/>
<link-entity name="contact" alias="ac" to="accountid" from="parentaccountid" link-type="outer"/>
<filter type="and">
<filter type="or">
<filter type="and">
<condition attribute="contact_renewaldate" value="60" operator="next-x-days"/>
<condition attribute="statecode" value="0" operator="eq"/>
<condition attribute="contact_renewal_frequency" value="908840003" operator="eq"/>
<condition entityname="ac" attribute="statecode" operator="ne" value="0"/>
</filter>
<filter type="and">
<condition attribute="contact_renewaldate" value="30" operator="last-x-days"/>
<condition attribute="statecode" value="0" operator="eq"/>
<condition attribute="contact_renewal_frequency" operator="in">
<value>908840002</value>
<value>908840001</value>
<value>908840004</value>
</condition>
<condition entityname="ac" attribute="statecode" operator="ne" value="0"/>
</filter>
</filter>
</filter>
</entity>
</fetch>

My SQL: Note that it doesn't include the 60 days - 30 days period but it does bring only Accounts that do not have active Contact

select name
from account a
where a.statecode = 0 and
not exists(
select *
from contact c
where c.parentaccountid = a.accountid
and c.statecode <> 0);

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Zibba,

    If you want accounts with no contacts, you can accomplish that easily by adding a left-join condition to your Fetch expression. I wrote up some details on that here: community.dynamics.com/.../451055

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Zibba360 Profile Picture
    on at

    Hi Aiden,

    Thanks for the reply. I don't only want the Accounts with no Contacts. Also the Accounts that have Contacts but all those Contacts are Inactive (None of the Account's Contacts is Active). For example, Account named Contoso has 3 Contacts in Joe, John, James. All of these Contacts have been Deactivated. Now that this Account has no Active Contact, bring me that Account in the FetchXML query so that we can appoint a consultant to manage this Account. If James is Activated again, do not bring me this Account in the query as James is managing this Account, even though Joe and John are still Inactive.

    I appreciate any help.

    Thanks

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Zibba,

    My suggestion will work for that query in FetchXml. Use a left join to account, with a statecode filter on the contact link. Then put a contactid null condition on the parent. Here's an example I wrote in the linked post - it will give you active accounts that don't have an appointment in the last 90 days. Your query would be nearly identical except you're linking to contacts (bolded the relevant parts):

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
      <entity name="account">
        <attribute name="name" />
        <attribute name="accountid" />
        <order attribute="name" descending="false" />
        <filter type="and">
          <condition attribute="statecode" operator="eq" value="0" />
          <condition entityname='appointment'
                  attribute='activityid'
                  operator='null'/>
        </filter>
        <link-entity name="appointment" from="regardingobjectid" to="accountid" alias="ab" link-type="outer">
          <filter type="and">
            <condition attribute="scheduledstart" operator="last-x-days" value="90" />
          </filter>
        </link-entity>
      </entity>
    </fetch

    If you want to run the same query in the advanced find, you can do it with a tool I helped write cobalt Cobalt Intelligent Query - free at http://www.cobalt.net/cobaltintelligentquery

    Thanks,

      Aiden

  • Zibba360 Profile Picture
    on at

    Hi Aiden,

    That helped, thanks!

  • specialKAMS Profile Picture
    44 on at

    Very helpful, thanks.  Any chance you could tell me how to add another condition that excludes SystemUser names with # in them?  Thx!!!

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Kim Creese,

    Here System user is:?

    1. Owner of the account
    2. Created On of the account
    3. Created By of the account
    4. Modified On of the account
    5. Modified By of the account
    6. Or Appointment...
  • specialKAMS Profile Picture
    44 on at

    Not quite what you mean. I am looking to run a query that give me logins (wide open on date) for all Dynamics 365 CE Users that are not true System like the flow users and stuff.  Those system users all have a "#" in the name.  I'm getting closer but need a way to exclude those lines of those system'y users altogether in order to get more of the results I want:

    <fetch>

     <entity name="audit" >

       <attribute name="createdon" />

       <attribute name="actionname" />

       <filter>

         <condition attribute="action" operator="eq" value="64" />

       </filter>

       <link-entity name="systemuser" from="systemuserid" to="objectid" link-type="outer" alias="SystemUser" >

         <attribute name="fullname" />

         <attribute name="businessunitid" />

         <filter>

           <condition attribute="fullname" operator="not-like" value="%#%" />

           <condition attribute="businessunitidname" operator="neq" value="prod-hg" />

           <condition attribute="territoryidname" operator="not-null" />

           <condition attribute="firstname" operator="not-null" />

         </filter>

       </link-entity>

     </entity>

    </fetch>

    Not sure if you know what I mean?  the results are returning all audit transaction with blank SystemUser.fullname cells for those with a #.  Seems to be tying up results and therefore will only give me logins back to Dec 17th.  I'm likely not explaining well?

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Kim Creese,

    Do you mean?

    pastedimage1671740153425v1.png

      
        
        
        
          
            
          
        
      
    

    or

    pastedimage1671740183533v3.png

      
        
        
        
        
          
            
          
        
      
    

  • specialKAMS Profile Picture
    44 on at

    Hmm, as you can see I already have the <condition attribute="fullname" operator="not-like" value="%#%" /> line in there (to exclude them) in the query but it is still giving me results of the lines that belong to users with them.  Looks like this below.  Is there a way to eliminate these lines altogether from results and only include the other User; I'm guessing I would get more results (i.e. dates further back) if they were not included.  Thanks.

    pastedimage1671741138471v1.png

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Kim Creese,

    Hm... Here you mean

    pastedimage1671741588018v1.png

      
        
        
        
        
        
        
        
          
            
          
        
      
    

    Am I right?

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans