Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

FetchXML - Not Exists Not Working

Posted on by 240

I've tried this a few different ways, trying to find someone who donated "Last Year", but not "This Year".

Not that it matters, but dates used for last year= 7/1/2020 to 6/30/2021, and date used for this year = 7/1/2021 to 6/30/2022.

Just trying to figure out the best way to do this when there are underlying "conditions" on both sides to the same entity.

Method one (Returns Rows, but includes people that donated in both years, along with those that didn't in some cases, not all, weird):

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="contact">
     <attribute name="contactid" />
     <attribute name="fullname" />
     <attribute name="emailaddress1" />
     <filter type="and">
          <condition attribute="statecode" operator="eq" value="0" />
          <condition entityname="aa" attribute="altai_fr_donationid" operator="not-null" />
          <condition entityname="bb" attribute="altai_fr_donationid" operator="null" />
     </filter>
     <link-entity name="altai_fr_donation" from="altai_contactid" to="contactid" link-type="outer" alias="aa">
          <attribute name="altai_fr_donationid" />
          <attribute name="altai_collecteddate" />
          <attribute name="altai_collectedamount" />
          <filter type="and">
               <condition attribute="altai_collecteddate" operator="on-or-after" value="@lfystartdate" />
               <condition attribute="altai_collecteddate" operator="on-or-before" value="@lfyenddate" />
          </filter>
     </link-entity>
     <link-entity name="altai_fr_donation" from="altai_contactid" to="contactid" link-type="outer" alias="bb">
          <attribute name="altai_fr_donationid" />
          <filter type="and">
              <condition attribute="altai_collecteddate" operator="on-or-after" value="@tfystartdate" />
              <condition attribute="altai_collecteddate" operator="on-or-before" value="@tfyenddate" />
          </filter>
      </link-entity>
</entity>
</fetch>

Method Two (Returns 0 rows always):

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='contact' >
     <attribute name='contactid' />
     <attribute name='fullname' />
     <attribute name='emailaddress1' />
     <filter type='and' >
          <!-- Contact In Active State -->
          <condition attribute='statecode' operator='eq' value='0' />
          <!-- LAST FISCAL YEAR - Donation in Active State, Donation EXISTS, On or after start selected, on or before end selected -->
          <condition entityname='lfy' attribute='statecode' operator='eq' value='0' />
          <condition entityname='lfy' attribute='altai_fr_donationid' operator='not-null' />
          <condition entityname='lfy' attribute='altai_collecteddate' operator='on-or-after' value='@lfystartdate' />
          <condition entityname='lfy' attribute='altai_collecteddate' operator='on-or-before' value='@lfyenddate' />
          <!-- THIS FISCAL YEAR - Donation in Active State, Donation DOES NOT EXIST, On or after start selected, on or before end selected -->
          <condition entityname='tfy' attribute='statecode' operator='eq' value='0'/>
          <condition entityname='tfy' attribute='altai_fr_donationid' operator='null' />
          <condition entityname='tfy' attribute='altai_collecteddate' operator='on-or-after' value='@tfystartdate' />
          <condition entityname='tfy' attribute='altai_collecteddate' operator='on-or-before' value='@tfyenddate' />
     </filter>
     <link-entity name='altai_fr_donation' from='altai_contactid' to='contactid' link-type='outer' alias='lfy'>
     </link-entity>
     <link-entity name='altai_fr_donation' from='altai_contactid' to='contactid' link-type='outer' alias='tfy'>
     </link-entity>
</entity>
</fetch>

  • jklemetsrud Profile Picture
    jklemetsrud 240 on at
    RE: FetchXML - Not Exists Not Working

    This is exactly what I needed, a fresh set of eyes on it. I miss the water cooler sometimes!! I was thinking about it from the contact side as my starting point rather than the donation side. Thankfully the client did want all the individual donation information listed and this works perfectly. Much Thanks!

  • Verified answer
    Adam Murabito Profile Picture
    Adam Murabito 30 on at
    RE: FetchXML - Not Exists Not Working

    One approach would be to retrieve Donations instead of Contacts.

    Consider the following dataset - in your use case, we'd want to return Randall and Jalen, because they donated last year but not this year, and exclude Donovan:

    pastedimage1655218532305v1.png

    You can achieve this by querying for donations in last fiscal year, and then joining the donations table back on the Contact. Your join will look for donations in this fiscal year. From there, you can filter and only include results where this fiscal year's donation date is null.

      
        
        
        
          
          
            
            
          
        
        
          
            
            
          
          
        
      
    

    Executing this fetch gives the following result, which we can tell corresponds to Randall and Jalen from the LFY donation dates:

    pastedimage1655219270685v2.png

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: FetchXML - Not Exists Not Working

    Hi John Klemetsrud,

    I think this is not possible with one query, maybe I am wrong.

    Step #1: You need to retrieve donors who donated last year.

    Step#2: You need to retrieve donors who contributed this year

    Step#3: On the basis of these two queries you have your results

    Let me know if I can you more details.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans