How to get Apex Page Messages size and use them in your code

By | March 4, 2016

Recently we were working on a code that involved two different classes. Our First Class consisted of try-catch block of code, try being used for carrying out the main purpose and catch basically to catch any exception if occurred. To know these exceptions we used Apex Page Messages to print them to precisely know what kind of exception or error we were supposed to handle. Now in our other class we were implementing the functionality of the first class and then implementing our function.

Real-time, bi-directional, multi-company – Sage ERP Integration with Salesforce

But while doing so we continuously encountered one problem of page crashing even though we tried catching the exception.

Img 1 VF error

Now the task ahead of us was to use Apex Page messages used in catch block from one class to other class. Now the point being they are just page messages they are used to display messages according to the developers demands; to keep a track of them from one class to another class is a big ask. After brain scratching and lot of R&D, we came across this simple method which we would like to share. Not many would know that you can actually get to know the size of page messages. So here’s what we came up with. In every catch block we already had page messages, now all we had to find out was whether these messages are getting populated in other class. We used conditional statements(If-Else) where we would check the size of page messages, below is snap of the syntax –Img 2 - Syntax on how to get size of Page messages

Now in If condition we are just checking the size, if there are page messages present then it won’t perform any function and just print the page messages but if no page messages are found than the code will carry on with its functionality. But there are few things to keep in mind – please use clear() method before finding the size of page messages. There are chances that your page messages might already be populated due to some other reason, so one needs to take care of this. Also its better to use as less messages as possible, i.e. use page messages where they are important like in catch block, to print some field validation and so on.

We hope this post helps you in finding the size of page messages in your code.

About Us
Greytrix as a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners across the globe. We offers Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that requires apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce integration with Sage Enterprise Management (Sage X3), Sage Intacct, Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data Migration, Integrated App development, Custom App development and Technical Support to business partners and end users.

Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star app listed on Salesforce AppExchange.

For more information, please contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts