1

I have created a VF page which is form to save account information. This page is visible to public and user with no logins can access it. I use this internally in my organization but I have plans to build login page to let only authorized person access it,but for now I have just this:

enter image description here

I use the Account standard controller in my VF page:

<apex:page standardController="Account" sidebar="false" showHeader="false">
    <apex:form >

    <apex:pageBlock title="Edit Account">
<apex:pageMessages/>
<apex:pageBlockSection columns="1">
    <apex:inputField value="{! Account.Name }"/>
    <apex:inputField value="{! Account.Phone }"/>        
    <apex:inputField value="{! Account.Industry }"/>        
    <apex:inputField value="{! Account.AnnualRevenue }"/>
</apex:pageBlockSection>

        <apex:pageBlockButtons >
            <apex:commandButton action="{! save }" value="Save" />        
        </apex:pageBlockButtons>

    </apex:pageBlock>


    </apex:form>
</apex:page>

When a user enters the details and click save. I get an authorization error Required page. When I use the page in Salesforce I am redirected to Account page once I click save. I assume that public users would not have access to this account page. How can I redirect this page to some other visual force page which says "Saved Successfully". Basically how to redirect a VF page to another VF page.

Answer:

I figured it out myself by reading the Salesforce's awesome documentation. I had to write a controller class(extensions) like below to redirect my page:

public class RedirectionClass{
    private final Account acct;

    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public RedirectionClass(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }

    public PageReference save() {
        insert acct;
        PageReference acctPage= new PageReference('http://hometest1-developer-edition.na34.force.com/test/helloworld');
        acctPage.setRedirect(true);
        return acctPage;
    }
}

1 Answer 1

1

The redirect is easy enough; you need to get in between the standard save method like this:

public with sharing class AccountSaveExtension {

    ApexPages.StandardController controller;

    public AccountSaveExtension(ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public PageReference doSaveAndRedirect() {
        this.controller.save();
        PageReference redirect = new PageReference('/success');
        return redirect;
    }

}

<apex:pageBlockButtons>
    <apex:commandButton action="{!doSaveAndRedirect}" value="Save" />        
</apex:pageBlockButtons>

But first, you have the bigger hairiness of the save not succeeding. You can try going to Setup > Sites, click Preview As Admin and then fill out your form again. Now the error should be displayed to you.

3
  • 1
    Thanks. Can you tell why we have to use a constructor in this class? What does it do?
    – SfdcBat
    Commented Nov 9, 2015 at 3:50
  • @rdvsm extensions exist to extend or decorate functionality onto controllers including standard controllers. Part of that architecture is that extensions are able to access the controller object instance. So it's injected in the constructor. I'm not sure why there isn't a base class or an interface for that, maybe for legacy reasons. Commented Nov 9, 2015 at 4:10
  • @rdvsm the {!save} in your visualforce page is calling the standard controller method. The apex code calling this.controller.save() is doing the equivalent. Commented Nov 9, 2015 at 4:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .