Monday, August 4, 2014

What is PageReference class in Salesforce?

What is PageReference class in Salesforce?

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:
· To view or set query string parameters and values for a page
·  To navigate the user to a different page as the result of an action method

Methods
PageReference methods are all called by and operate on a particular instance of a PageReference 

The table below describes the instance methods for PageReference.


Controller :

public class PageReferenceController {
Account account;
    public Account getAccount() {
        if(account == null) account = new Account();
        return account;
    }

    public PageReference save() {
        // Add the account to the database. 
        insert account;
        // Send the user to the detail page for the new account.
        PageReference acctPage = new ApexPages.StandardController(account).view();
        acctPage.setRedirect(true);
        return acctPage;
    }
    
    
}


In controller class once the account record details is entered and click on save button the PageReference will redirect you to Newly created Account record detail page using the 

 PageReference acctPage = new ApexPages.StandardController(account).view();
 acctPage.setRedirect(true);



Visualforce Page:

<apex:page controller="PageReferenceController" tabStyle="Account">
    <apex:sectionHeader title="New Account Edit Page" />
    <apex:form >
        <apex:pageBlock title="Create a New Account">
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Account Information">
                <apex:inputField id="accountName" value="{!account.name}"/>
                <apex:inputField id="accountSite" value="{!account.site}"/>
            </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>


Output:

save image

 once you click on save button it will save the record and display in record detail page.


save image





That's it ..
Thanks for Reading...







0 comments:

Post a Comment

 
| ,