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?
it worked
hi Tilan, did the above work for you?
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
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?
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.
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 -%}
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?
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Rishabh Kanaskar 258
MVP-Daniyal Khaleel 164
Tom_Gioielli 121 Super User 2025 Season 2