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 :
Customer experience | Sales, Customer Insights,...
Answered

Liquid - count the results returned by fetchxml

(0) ShareShare
ReportReport
Posted on by

I need to count the results returned by fetch xml in liquid and if 0 records are returned then need to execute a javascript code, How can i do this?

I have the same question (0)
  • Fabio Branco Profile Picture
    on at

    Hello Tilan,

    Thank you for your question

    You should be able to count the total elements of the response from the Collection.

    The following community thread has directions with the steps to count the results from the QueryExpression:

    Allow me to share aditional documentation with th steps to count the records of the Collection from the WEB API:

    Does this information answer your question? 

  • Suggested answer
    oliver.rodrigues Profile Picture
    4,052 on at

    hi Tilan

    you should be able to get your result count with no issues using liquid/fetch

                          {%- fetchxml myFetch -%}

                           <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">

                               <entity name="xxxxxx">

                                   <attribute name="xxxx" />

                               </entity>

                           </fetch>

                           {%- endfetchxml -%}

                           {%- assign fetchResult = myFetch.results.entities -%}

                           {%- if fetchResult.size > 0 -%}

                           {% endif %}

  • SOUNDHARYA.N.V Profile Picture
    165 on at

    Hi,

    Try this,

    {% fetchxml myFetch %}
    <fetch version="1.0" mapping="logical">
    <entity name="name">
    <attribute name="name" />
    <attribute name="name" />
    <attribute name="createdon" />
    <order attribute="name" descending="false" />
    </entity>
    </fetch>
    {% endfetchxml %}

    {% if myFetch.results.entities.size > 0 %}

    // perform your logic here

    {% endif %}

    if you want to use in Javascript if condition then assign the results and use below code

    {% assign result = myFetch.results.entities %}

    <script>
    if(result.size > 0)
    {
    // perform your logic here
    }
    </script>

    Please mark as verified if the answer is helpful. 

  • Tilan Bethmage Profile Picture
    on at
    [deleted]
  • Suggested answer
    oliver.rodrigues Profile Picture
    4,052 on at

    hi, you can remove the hyphen "-" from the liquid tags, although I don't think that's the problem

    is the issue when you are copying the code? have you tried just re-write it?

    did you add a "div" with any content inside the IF just to see if it is hitting that point?

  • Tilan Bethmage Profile Picture
    on at

    Hi Oliver,

    When i add the if statement it becomeslike {% if fetchResult.size  "& g t; "   0 %}

    with & and gt so its not working, not sure why

  • Verified answer
    oliver.rodrigues Profile Picture
    4,052 on at

    hi Tilan, did the above work for you?

  • Tilan Bethmage Profile Picture
    on at

    it worked

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 > Customer experience | Sales, Customer Insights, CRM

#1
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 60 Super User 2025 Season 2

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans