Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I want to redirect from a visualforce page to a custom obect's list page. I need to run a method in the controller as th action for the button, so I would like to redirect from the controller.

I found this doc on how this doc on how to redirect to a standard object list page, but I need to redirect to a 'Custom Object's List Page', and I need to do it from a controller, not the VF page itself.

Is this possible?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

You could get the object prefix, which returns the first 3 characters in the object's ID. Then construct an URL and redirect to it.

Example redirect to Account List View:

public PageReference redirectToList() {
    Schema.DescribeSObjectResult result = Account.SObjectType.getDescribe();

    PageReference pageRef = new PageReference(‘/’ + result.getKeyPrefix());
    pageRef.setRedirect(true);
    return pageRef;
}

If you need to get to the landing page append a '/o' after the result.getKeyPrefix():

PageReference pageRef = new PageReference(‘/’ + result.getKeyPrefix() + '/o');
share|improve this answer
    
Thank you, This works perfect! –  LShropshire Jan 24 at 10:21
2  
result.getKeyPrefix() is actually a tighter solution than my String/Substring snippet... Great answer. (If only I hadn't spent all that time on screenshots!) –  Simon Lawrence Jan 24 at 10:33
add comment

You can link to the "tab" style page (I figure this is the list page you are asking about) by redirecting to the first three characters of the record ID... as this represents the object type.

As you can see here the ID of my custom test object....

object page

The first three characters is the list page URL

list page

So if you had a page, with a custom controller, with an object in it, stick a button on the VF page that calls your action

<apex:commandButton value="GO!" action="{!buttonGo}" />

And then in your controller, you can do something like this to send the user off to the relevant URL

public with sharing class testExtensions {

    public Test_Object__c tester { get; set; }

    public testExtensions(ApexPages.StandardController controller) {
        this.tester = (Test_Object__c)controller.getRecord();
    }

    public PageReference buttonGo()
    {
        PageReference pr = new PageReference('/' + String.valueOf(tester.Id).substring(0,3));
        pr.setRedirect(true);
        return pr;
    }

}

... You haven't said what is in your controller/what it does, so I can't speculate any better than that?? Hopefully this is enough to get your over the line though!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.