+ Start a Discussion
j_r_roj_r_ro 

Custom exception not added to ApexPages page message

I am trying to display a custom exception to the user, using ApexPages.addMessage(msg) and display it in an apex:pageMessages block.

 

I am adding this message like this:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, message);
System.debug('My message: ' + myMsg);

ApexPages.addMessage(myMsg);

System.debug('Exception handler messages: ' + ApexPages.getMessages());

 

 

In the first debug statement, the error is logged as expected:

ApexPages.Message["Custom exception message.
Error message: Update failed. First exception on row 0 with id; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Required test]: [Required test]"]

 

However, when I log out the messages list, only the 'REQUIRED_FIELD_MIssing error is logged, and dislpayed on the page.

 

When I debug the ApexPages.getMessages() BEFORE and AFTER calling addMessage(custom message), the messages list only contains the Salesforce-generated message.

The custom message has not been added.

 

 

 

Himanshu ParasharHimanshu Parashar
Hi,
I think it is correct behavior, SFDC first execute the Page UI validation rules and then move to controller side.

Try to disable the required true attribute from the page fields, it should show you your custom message.
j_r_roj_r_ro

The required field error is correct.

 

The behaviour catches this error, and creates a custom message related to the field update failure.

 

However, the custom message is not being added to ApexPage messages.

 

When I debug the ApexPages.getMessages() BEFORE and AFTER calling addMessage(custom message), the messages list only contains the Salesforce-generated message.

The custom message has not been added.

Himanshu ParasharHimanshu Parashar
are you rendering the page message block ?
j_r_roj_r_ro

Yes. The 'Required field missing' error is shown on the page, but the custom error is missing, as it is has not been added to page messages.