Understanding Component in Visualforce

The <apex:pageMessage> component is used to display simple messages on a Visualforce page. It is typically used for success, error, or information messages that need to be shown to users.


Syntax

xmlCopyEdit<apex:pageMessage summary="Your message here" severity="info" />

Key Attributes

AttributeDescription
summaryThe text message you want to display.
severityThe type of message to display (info, error, warning, success).
detailOptional. Provides additional detail for the message.
renderedOptional. Determines whether the message is visible.

Example 1: Basic Usage

xmlCopyEdit<apex:page>
    <apex:pageMessage summary="This is an informational message." severity="info" />
</apex:page>
  • This will display an informational message on the page.

Example 2: Success Message

xmlCopyEdit<apex:page>
    <apex:pageMessage summary="Your data has been saved successfully!" severity="success" />
</apex:page>
  • A success message shows up after an action (like saving data) is completed.

Example 3: Error Message with Detail

xmlCopyEdit<apex:page>
    <apex:pageMessage summary="Error occurred during the process!" severity="error" detail="Unable to connect to the database." />
</apex:page>
  • Error messages can include detailed explanations to guide users.

When to Use <apex:pageMessage>

  • Display status or result messages after form submission or actions.
  • Show important user feedback (errors, warnings, or success notifications).

Best Practices

  • Use severity="info" for general messages and severity="error" for critical issues.
  • Avoid overloading users with too many messages; use sparingly to avoid clutter.
  • Always use summary for concise messages and detail for additional information.