How to Check for 0 Results in VisualForce

Sharing is Caring

In VisualForce, debugging errors can be pretty challenging at times. It’s usually best to handle before they could potentially occur.

Checking if a list is null and has no results is pretty easy. The best way to do this is to use the ISNULL operator and and to check if the results has a size that isn’t 0.

Here’s how to check if a list called results isn’t null.

<apex:pageBlockTable value="{!results}" var="l" rendered="{!NOT(ISNULL(results))}">

Here’s how to check if a list called results has a size > 0. This works, because lists can’t have a negative size. I do this because I find that in some cases the greater than ( >) and less than (<) operators can mess with the rendered attribute.

<apex:pageBlockTable value="{!results}" var="l" rendered="{! results.size != 0)}">

We can then combine these two conditions together and have a pretty bulletproof solution that won’t crash the page.

<apex:pageBlockTable value="{!results}" var="l" rendered="{! NOT(ISNULL(results)) && (results.size != 0))}">

And of course, we can even add a page block instead that renders when there’s no results and tells the user that.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.