function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jjulianjjulian 

Redirecting to an objects list view from APEX controller.

From within my apex controller, how do set PageReference to redirect to an object's list view page?

 

The only documented way of doing this is through visualforce:

 

<apex:page action="{!URLFOR($Action.Account.List, $ObjectType.Account)}"/>

 However, I want to be able to do this from the controller instead:

public PageReference confirm()
{
	// Do prep data
	
	PageReference ref = new PageReference();
	
	// ??? redirect to object's list view

	return ref;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Gunners_23Gunners_23

There is one more way its kind of kludgy. Hardcode the id of the list view (not recommended but it'll work for sure)

 

Go to the tab and click edit on view which you want to redirect to copy id. And then in the controller use the following code

 

PageReference ref = new PageReference(/{objectprefix}?fcf=00BO0000000LhtN);

 

for account : PageReference ref = new PageReference(/001?fcf={ViewID});

 

 

All Answers

Gunners_23Gunners_23

There is one more way its kind of kludgy. Hardcode the id of the list view (not recommended but it'll work for sure)

 

Go to the tab and click edit on view which you want to redirect to copy id. And then in the controller use the following code

 

PageReference ref = new PageReference(/{objectprefix}?fcf=00BO0000000LhtN);

 

for account : PageReference ref = new PageReference(/001?fcf={ViewID});

 

 

This was selected as the best answer
Andrew B. DavisAndrew B. Davis
A more flexible solution that will work even if you deploy this code to/from a Sandbox or other Org is to use the getKeyPrefix function to figure out what the correct prefix is for your object. This is described in Salesforce.Stackexchange.com (http://salesforce.stackexchange.com/questions/25027/can-you-redirect-to-a-custom-object-list-page-from-a-controller). Here's the proposed code:
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');
Andrew B. DavisAndrew B. Davis
The solution from stackexchange is also presented in this Blog post: How to redirect to List View page from Apex Controller (http://blog.cloudexult.com/2013/06/12/how-to-redirect-to-list-view-page-from-apex-controller/)
Ikbal HalakeriIkbal Halakeri
Hi All,

I think hardcoding the URL with ID's its not betterway, and above mentioned one might fail, if the user switched to lightning experience. 
public PageReference redirectToList() { 
     return new ApexPages.Action('{!List}').invoke();
}
This will work in both classis and lightning.