function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Austin MayerhoferAustin Mayerhofer 

Display VF content based on if statement?

Hi all,

I'm trying to display a table of values on my VF page based on if I have 1 or more objects in the backend for a custom object pending_food__c. If there's pending_food__c objects that exist, display a table, if there's not, don't display a table.

For example, I'm trying to achieve this by doing:
<apex:pageBlockSectionItem rendered="{![SELECT Id FROM pending_food__c].size() > 0}">     
</apex:pageBlockSectionItem>
But I'm getting " Syntax error.  Missing '=' ". How can I implement the functionality of displaying based on whether there's pending_food__c's in the backend?
Best Answer chosen by Austin Mayerhofer
Abhishek BansalAbhishek Bansal
Hi Austin,

It is not allowed to do the query on a VF page. You should use a controller class for this VF page and than execute the SOQL in this class:
Consider the below example:
VF Page:
<apex:page controller"MyControllerClass">
......
.....
<apex:pageBlockSectionItem rendered="{!listOfPendingFoods.size > 0}">     
</apex:pageBlockSectionItem>

.........
........
</apex:page>

//Class:
public MyControllerClass{
	public List<pending_food__c> listOfPendingFoods {get;set;}
	
	public MyControllerClass() {
		listOfPendingFoods = new List<pending_food__c>();
		listOfPendingFoods = [SELECT Id FROM pending_food__c];
	}
}

Let me know if you need any further help on this.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102