Apex Page Message in Visualforce

To display a page message on a Visualforce page, you can use the <apex:pageMessages> component. This component is typically used to display messages to users, such as validation errors, informational messages, or confirmation messages. Here's how you can use it:

<apex:page controller="YourControllerName">
    <!-- Your Visualforce page content goes here -->
    
    <!-- Page messages section -->
    <apex:pageMessages />

</apex:page>

Within your Apex controller or controller extension, you can add messages to be displayed using the Apex addError() method. For example:

public class YourControllerName {
    public PageReference yourActionMethod() {
        // Perform some action
        
        // Add a message
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Your message here.'));
        
        // Redirect to another page or return null
        return null;
    }
}

In this example, when yourActionMethod() is executed, it adds an informational message to the page messages, which will be displayed when the page is re-rendered. You can customize the severity of the message by replacing ApexPages.Severity.INFO with ERROR, WARNING, or CONFIRM.

You can also add messages directly without using the controller. For instance:

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Your message here.'));

This approach can be useful if you need to display messages without performing any action in the controller.

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach