Thursday, June 29, 2017

apex:pagemessage:


How to use apex:pageMessage in Visualforce Page?

How to use <apex:pageMessage>  in Visualforce Page?

<apex:pageMessage> :
This component should be used for presenting custom messages in the page using the Salesforce pattern for errors, warnings and other types of messages for a given severity. See also the pageMessages component.


How to write message class in apex controllers and Visualforce pages?


How to write message class in apex controllers and Visualforce pages?

Message Class:
Whenever we perform validations in our custom or extension controllers, we would need to display error messages if the user enters invalid data and these error messages can be constructed and displayed with the help of this Message class.

If your application uses a custom controller or extension, you must use the message class for collecting errors.
Syntax:
In a custom controller or controller extension, you can instantiate a Message in one of the following ways:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary);

where ApexPages.severity is the enum that is determines how severe a message is, and summary is the String used to summarize the message.
For example:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg');

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary, detail);

where ApexPages.severity is the enum that is determines how severe a message is, summary is the String used to summarize the message, and detail is the String used to provide more detailed information about the error.

ApexPages.Severity Enum: 
Using the ApexPages.Severity enum values, specify the severity of the message. The following are the valid values:

1. CONFIRM
2. ERROR
3. FATAL
4. INFO
5. WARNING

All enums have access to standard methods, such as name and value.


Page Reference Class:
A PageReference is a reference to an instantiation of a page. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values.
Use a PageReference object:

1. To view or set query string parameters and values for a page
2. To navigate the user to a different page as the result of an action method

Syntax:
     In a custom controller or controller extension, you can refer to or instantiate a PageReference in one of the following ways:

Page.existingPageName     

Refers to a PageReference for a Visualforce page that has already been saved in your organization. By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists.

PageReference pageRef = new PageReference('partialURL');

Creates a PageReference toany page that is hosted on the Force.com platform. For example, setting 'partialURL' to'/apex/HelloWorld' refers to the Visualforcepage located at http://mySalesforceInstance/apex/HelloWorld. Likewise, setting 'partialURL' to '/' + 'recordID' refers to the detail page for the specified record.

This syntax is less preferable for referencing other Visualforce pages than Page.existingPageName because the PageReference is constructed at runtime, rather than referenced at compile time. Runtime references are not available to the referential integrity system. Consequently, the platform doesn't recognize that this controller or controller extension is dependent on the existence of the specified page and won't issue an error message to prevent user deletion of the page.

PageReference pageRef = new PageReference('fullURL');

Creates a PageReference for an external URL. For example:

PageReference pageRef = new PageReference('http://www.google.com');

You can also instantiate a PageReference object for the current page with the currentPage ApexPages method. For example:

PageReference pageRef = ApexPages.currentPage();


Select Option Class:

A SelectOption object specifies one of the possible values for a Visualforce selectCheckboxes, selectList, or selectRadio component. It consists of a label that is displayed to the end user, and a value that is returned to the controller if the option is selected. A SelectOption can also be displayed in a disabled state, so that a user cannot select it as an option, but can still view it.

Syntax:
     In a custom controller or controller extension, you can instantiate a SelectOption in one of the following ways:

SelectOption option = new SelectOption(value, label, isDisabled);

where values is the String that is returned to the controller if the option is selected by a user,label is the String that is displayed to the user as the option choice, andis Disabled is a Boolean that, if true, specifies that the user cannot select the option, but can still view it.

SelectOption option = new SelectOption(value, label);

where value is the String that is returned to the controller if the option is selected by a user, and label is the String that is displayed to the user as the option choice. Because a value for is Disabled is not specified, the user can both view and select the option.

Standard Controller Class:
StandardController objects reference the pre-built Visualforce controllers provided by salesforce.com. The only time it is necessary to refer to a StandardController object is when defining an extension for a standard controller. StandardController is the data type of the single argument in the extension class constructor.

Syntax:
You can instantiate a StandardController in the following way:
ApexPages.StandardController sc = new ApexPages.StandardController(sObject);



This tag supports following attributes:

Attribute
Description
detail
The detailed description of the information.
escape
A Boolean value that specifies whether sensitive HTML and XML characters should be escaped in the HTML output generated by this component. If you do not specify escape="false", the character escape sequence displays as written. Be aware that setting this value to "false" may be a security risk because it allows arbitrary content, including JavaScript, that could be used in a malicious manner.
id
An identifier that allows the component to be referenced by other components in the page.
rendered
A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.
severity
The severity of the message. Values supported are: 'confirm', 'info', 'warning', 'error'
strength
The strength of the message. This controls the visibility and size of the icon displayed next to the message. Use 0 for no image, or 1-3 (highest strength, largest icon).
summary
The summary message.
title
The title text for the message.


Visualforce Example:

<apex:page standardController="Opportunity" recordSetVar="opportunities" tabStyle="Opportunity" sidebar="false">
    <p>Enter an alphabetic character for the "Close Date," then click Save to see what happens.</p>
    <apex:form >
        <apex:pageBlock >
        <apex:pageMessage summary="This pageMessage will always display. Validation error 
           messages appear in the pageMessages component." severity="warning" strength="3" />
        <apex:pageMessages />
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}"/>
        </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!opportunities}" var="opp">
                <apex:column value="{!opp.name}"/>
                <apex:column headerValue="Close Date">
                    <apex:inputField value="{!opp.closeDate}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>




Show error message in Visualforce Page using ApexPages.addmessage


Sometime we need to show  or display error message on Visualforce page with different notations like warning, error, info etc.... We can implement this requirement by creating new instance of ApexPages.message and then adding message to Apexpages using ApexPages.addmessage. Then displaying these messages in visualforce page.

We can display 5 different types of message in Visualforce Page. In the example below, we are showing 5 input fields of account. We have added a button on visualforce page. Different type of message will be shown on visualforce page if we will keep any field blank.

         



Visualforce Page:

<apex:page standardController="Account" extensions="DisplayErrorMessageInVfCls">
 <apex:form >
   <apex:pageblock >
      <apex:pageMessages id="showmsg"></apex:pageMessages>
         <apex:panelGrid columns="2">
           Account Name: <apex:inputText value="{!acc.name}"/>
           Account Number: <apex:inputText value="{!acc.AccountNumber}"/>
           Account Phone: <apex:inputText value="{!acc.phone}"/>
           Account Site: <apex:inputText value="{!acc.site}"/>
           Account Industry: <apex:inputText value="{!acc.industry}"/>
           <apex:commandButton value="Save Details" action="{!save}" style="width:90px" rerender="showmsg"/>
         </apex:panelGrid>
    </apex:pageblock>
 </apex:form>
</apex:page>


Apex Class:

public with sharing class DisplayErrorMessageInVfCls{

    public Account acc{get;set;}
    //extension of Standard controller.
    public DisplayErrorMessageInVfCls(ApexPages.StandardController controller) {
        acc = new Account();
    }
    public void save(){
      if(acc.name == '' || acc.name == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
      if(acc.AccountNumber == '' || acc.AccountNumber == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
      if(acc.phone == '' || acc.phone == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));
      if(acc.site == '' || acc.site == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));
      if(acc.industry == '' || acc.industry == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry'));
    }
}



To Refer visualforce tags :


No comments:

Post a Comment