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

ApexPages.Message - soql query governor limit

I'm trying to catch a soql query governor limit and display using ApexPages.Message, but it doesn't display the message correctly in my VF page.


Here is my code:


<apex:page standardController="Project_Request__c" extensions="ProjectRequestExceptionsExtensionVF" tabStyle="Project_Updates__tab">
	<apex:sectionHeader title="Project Request Exceptions:" subtitle="{!UserName}" />
	<apex:pageMessages />
	<apex:pageMessage title="Q: What is the purpose of this page?" severity="info" strength="2" >
		<apex:outputPanel layout="block">A: This page will alert you to any of your Projects that need reviewing and updating.  In addition to routinely monitoring your Projects in Salesforce, this page should make it easier to quickly identify Projects that need attention.</apex:outputPanel>
	<apex:form id="pageForm">
    	<apex:pageBlock >
    		<apex:pageBlockTable value="{!Projects}" var="prj" id="theTable1" rowClasses="odd,even">
    			<apex:column >
    				<apex:facet name="header">
    				<apex:commandLink action="{!URLFOR($Action.Project_Request__c.Edit, prj.Id, [retURL=URLFOR('/apex/vfProjectRequestExceptions')],true)}" value="Edit" />
    			<apex:column >
                	<apex:facet name="header">
                    	Project Request #
                	<apex:outputLink value="/{!prj.Id}" >{!prj.Name}</apex:outputLink>
            	<apex:column >
            		<apex:facet name="header">
            			Opportunity Name
            		<apex:outputLink value="/{!prj.Opportunity__r.Id}" >{!prj.Opportunity__r.Name}</apex:outputLink>
            	<apex:column >
            		<apex:facet name="header">
            			Exception Report - Message 1
            		<apex:outputText value="{!prj.Exception_Report_Message_1__c}" />
            	<apex:column >
            		<apex:facet name="header">
            			Exception Report - Message 2
            		<apex:outputText value="{!prj.Exception_Report_Message_2__c}" />


public class ProjectRequestExceptionsExtensionVF {
	private List<Project_Request__c> projs;
	private String userFirstName;
	private String userLastName;

	public ProjectRequestExceptionsExtensionVF(ApexPages.StandardController stdController) {
		userFirstName = userinfo.getFirstName();
		userLastName = userinfo.getLastName();
	public List<Project_Request__c> getProjects() {
			try {
				projs = [Select Id, Name, Opportunity__r.Id, Opportunity__r.Name, Exception_Report_Message_1__c, Exception_Report_Message_2__c,
							Exception_Report_Actual_Start_Date__c, Exception_Report_Opp_Exp_Start_Date__c From Project_Request__c Where 
							(Exception_Report_Actual_Start_Date__c = 1 OR Exception_Report_Opp_Exp_Start_Date__c = 1 OR 
							Exception_Report_Post_Consulting_Call__c = 1 OR Exception_Report_Pricing_Required__c = 1 OR 
							Exception_Report_Report_Due_Date__c = 1 OR Exception_Report_Survey_Sent__c = 1) AND (Opportunity__r.StageName != 'Closed Lost' OR
							Opportunity__r.StageName != 'Rejected' OR Opportunity__r.StageName != 'Cancelled') AND Project_Status__c != 'Cancelled' AND 
							My_Project__c = 1 AND Opportunity__r.CreatedDate > 2009-11-01T23:01:01+01:00];
				return projs;
			catch (Exception ex) {
				ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
				return null;	
	public String getUserName() {
		String strName = 'User - ' + userFirstName + ' ' + userLastName;
		return strName;

	static testMethod void test() 
    	RecordType testRecordType = [SELECT	id, Name, DeveloperName FROM RecordType WHERE	Name = 'Billable Project'];

		Opportunity opp = new Opportunity(Name = 'Acct Name - Test Opportunity A2', CloseDate =, Clients_Expected_Start_Date__c = + 1, StageName = 'Project Request', RecordType = testRecordType, Probability = 10);
		insert opp;
		Project_Request__c pr = new Project_Request__c(Opportunity__c = opp.Id, Department__c = 'Information Technology');
		insert pr;
        ApexPages.StandardController ctl = new ApexPages.StandardController(pr);
        ProjectRequestExceptionsExtensionVF Ctrl = new ProjectRequestExceptionsExtensionVF(ctl);


If the query returns more than 1000 records, I want to catch the error and display at the top of the VF page that the query limit has been exceeded.  I can put in a limit clause on the query to a 1000, but can't I catch the error?


One way is to put your code in a try-catch. It will handle any exception related with the governer limits. The other way is to use functions like Limits.getDMLRows() and Limits.getDMLStatements(). These functions will let you know the actual rows and DML statements used in your code. You can compare the result with the actual governor limits and handle the code.


Hope this helps.          


I am using a try/catch block, but the message isn't displayed in the pageMessages control.  Why?


Could be wrong, but I think you need to initialize the project list prior to rendering the page.  Without mocking up a test in my own org, I think what's happening is the pageMessages component is rendered before the message is added.


You could either initialize the list in the controller's constructor, or call an action method in the page component (e.g. <apex:page ... action="{!something}">).