anusha(salesforce developer)

Monday 1 August 2016

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);


No comments:

Post a Comment