Play Games

Search This Blog

Thursday, July 7, 2016

PageReference in Salesforce.

A PageReference is a reference to an instantiation of a page.
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.

Use below code to navigate to specified url.
PageReference pageRef = new PageReference('specified url');

Use below code syntax to navigate to external url for example:google
PageReference pageRef = new PageReference('http://www.google.com');
Example:
Apex Class:
public class PageReferenceExample {
    Account account;
    public Account getAccount() {
        if(account == null) account = new Account();
        return account;
    }

    public PageReference save() {
        insert account;
        PageReference acctPage = new ApexPages.StandardController(account).view();
        acctPage.setRedirect(true);
        return acctPage;
    }

}
Visualforce Page:
<apex:page controller="PageReferenceExample" 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:When we run the above vf page the following screen appears

Once you enter the details,Click on save button.

When a save button it is redirected to the record detail page as shown in the image below.




No comments:

Post a Comment