PDI · Question #5
A developer has a Visualforce page and custom controller to save Account records. The developer wants to display any validation rule violation to the user. How can the developer make sure that validat
The correct answer is B. Include <apex:message> on the Visualforce page.. The developer wants to display validation rule violations from a custom controller to a Visualforce page when saving Account records.
Question
A developer has a Visualforce page and custom controller to save Account records. The developer wants to display any validation rule violation to the user. How can the developer make sure that validation rule violations are displayed?
Options
- AAdd cuatom controller attributes to display the message.
- BInclude apex:message on the Visualforce page.
- CUse a try/catch with a custom exception class.
- DPerform the DML using the Database.upsert() method.
How the community answered
(26 responses)- A8% (2)
- B88% (23)
- D4% (1)
Why each option
The developer wants to display validation rule violations from a custom controller to a Visualforce page when saving Account records.
Custom controller attributes are not the standard or simplest way to display system-generated validation rule messages; Visualforce provides dedicated components for this purpose.
The `<apex:message>` or `<apex:messages>` Visualforce components are designed to automatically display all error messages, including those generated by standard validation rules and Apex `addError()` calls, when a DML operation fails due to validation.
While `try/catch` blocks handle exceptions, validation rule violations are platform-level errors that are best displayed using `<apex:messages>` or `<apex:message>` directly, rather than relying on a custom exception class for this specific scenario.
`Database.upsert()` is a DML operation for inserting or updating records, but it does not inherently handle the display of validation errors on a Visualforce page; it would return `Database.SaveResult` objects that would need manual processing to extract and display errors.
Concept tested: Visualforce error message display
Source: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_messages.htm
Topics
Community Discussion
No community discussion yet for this question.