How to Check for 0 Results in VisualForce

Salesforce Logo

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