1

I have a Visualforce page that when there is more than one field error it only displays the first error in the apex:pageMessages section.

The Salesforce documentaion says all the pageMessages should display all the error messages.

How can I get all the errors to display?

Validation logic.

if (c.Subject == null) {
    valid = false;
    c.Subject.addError('You must enter a value');
}
if (c.ContactId == null) {
    valid = false;
    c.ContactId.addError('You must enter a value');
}
if (c.Priority == null) {
    valid = false;
    c.Priority.addError('You must enter a value');
}
if (c.Description == null || c.Description == '') {
    valid = false;
    c.Description.addError('You must enter a value');
}
if (!valid) {
    return null;
}

3 Answers 3

3

The error messages must be unique.

Salesforce treats error messages that are exactly the same as the same message and only displays it once.

I changed the error messages from "You must enter a value" to "You must enter a subject" "You must enter a description" etc. and it works correctly.

1

As per Salesforce documentation. <apex:pageMessages component displays all messages that were generated for all components on the current page, presented using the Salesforce styling.

You have to add <apex:pageMessages /> in the VF page. And in controller you have to add ApexPages.Message where ever required. All error messages will be displayed as in the image below.

Visualforce page:

<apex:pageMessages />

Apex Controller:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');
ApexPages.addMessage(myMsg);

(Or)

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.'));

Error Examples:

catch (Exception e) {
    String error = e.getMessage();
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, error));
}

Info Example:

if (some_condition) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Info not an error'));
}
if (ApexPages.hasMessages(ApexPages.Severity.Info)) {
    // do something
}

Sample screenshot: enter image description here

3
  • This works for displaying all page errors. If you add errors to a field it will only show 3 of them.
    – Fred B
    Commented Aug 27, 2015 at 18:43
  • If possible can you please add a screenshot in your question.
    – Saroj Bera
    Commented Aug 27, 2015 at 18:51
  • thanks for your help. I found out it was an issue with using the same error message on each field. When building a new page for the screen shot I found this issue.
    – Fred B
    Commented Aug 27, 2015 at 20:31
0

You can use following code to display all error massages.

<apex:page controller="TestMessageController">
  <apex:pageMessages />
</apex:page>

public class TestMessageController{
    public TestMessageController(){
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This is apex:pageMessages'));
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This is apex:pageMessages still'));
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'This is apex:pageMessages info'));
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'This is apex:pageMessages warning'));
    }
}

Source - Link

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .