開発者には、取引先レコードを保存するための Visualforce ページとカスタム コントローラがあります。開発者は、検証ルール違反をユーザーに表示したいと考えています。
開発者は検証ルール違反が表示されるようにするにはどうすればよいでしょうか?
正解:A
When saving records using a custom controller in a Visualforce page, validation rule violations can occur. To display these validation error messages to the user, the developer should use the <apex:messages> component.
Option A: Include <apex:messages> on the Visualforce page.
Correct Approach.
The <apex:messages> component displays all messages that were generated for all components on the page, including validation rule violations.
When a DML operation is performed, any validation errors are automatically added to the ApexPages message collection.
Including <apex:messages> in the page ensures that these messages are displayed to the user.
Example:
<apex:page controller="CustomAccountController">
<apex:form>
<apex:messages />
<!-- Form fields for Account -->
<apex:commandButton action="{!saveAccount}" value="Save" />
</apex:form>
</apex:page>
While a try/catch block can catch exceptions, it is not required for displaying validation errors.
Validation rule violations are added to the message collection automatically.
Using try/catch may suppress the standard error handling.
Option C: Add custom controller attributes to display the message.
Inefficient Approach.
Manually adding controller attributes and logic to handle error messages adds unnecessary complexity.
The standard <apex:messages> component handles this automatically.
Option D: Perform the DML using the Database.upsert() method.
Not Sufficient Alone.
Using Database.upsert() allows for finer control over DML operations and error handling.
However, unless the errors are added to the message collection, they will not be displayed.
The developer would still need to handle displaying the errors.
Conclusion:
By including the <apex:messages> component on the Visualforce page, validation rule violations and other error messages are displayed to the user automatically.
Therefore, Option A is the correct answer.
Reference:
apex
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_messages.htm Displaying Errors Option B: Use a try/catch with a custom exception class.
Not Necessary.