Using the default presentation mechanisms, only the object's Name field is presented with a hyperlink (that makes use of the object's ID). So to get that link back you just need to put back the auto-number field (which I assume is the Name field). There is no ability to control the default sort of remove the sorting. The positive of using these default mechanisms is that your tab behaves like many other tabs which is usually helpful for users.
If more specific behaviour is important to you, you can write your own Visualforce list page based on the StandardSetController and override the object's tab action to use that page. Then you can hyperlink whichever field you like and lock down the sort order.
I don't entirely agree with implementing JS as the solution, but independent of that, you can use a Hierarchy Custom Setting (documentation) and merge in a value from it:
parent.frames.location.replace("{!$Setup.Order_Settings__c.Recently_Viewed__c}");
EDIT
You could also get the Id
more programmatically by using ApexPages.StandardSetController
. Take a look at the getListViewOptions
method, it gives you back a List<SelectOption>
. Each represents a List View
with value=Id
and label=Name
.
Service
The below is a rudimentary and generic attempt to pair an sObjectType
with a List View Name
and come up with its Id
and also a corresponding URL
.
public with sharing class ListViewServices
{
public static String getId(SObjectType sObjectType, String name)
{
List<SObject> records = new List<SObject> { sObjectType.newSObject(); }
ApexPages.StandardSetController controller =
new ApexPages.StandardSetController(records);
for (SelectOption listView : controller.getListViewOptions())
{
if (listView.getLabel() == name) return listView.getValue();
}
return '';
}
public static String getUrl(String sObjectType, String name)
{
if (String.isBlank(sObjectType) || String.isBlank(name)) return null;
SObjectType schemaType = Schema.getGlobalDescribe().get(sObjectType);
this.relativeBaseUrl = String.format('/{0}?fcf={1}', new List<String> {
schemaType.getDescribe().getKeyPrefix(),
getId(schemaType, name)
});
}
}
Controller
Using the above service in a specific controller should be trivial.
public with sharing class ListViewController
{
public static final String TYPE_PARAM = 'sobjecttype';
public static final String NAME_PARAM = 'name';
public String listViewUrl { get; private set; }
public ListViewController()
{
this.listViewUrl = ListViewServices.getUrl(
ApexPages.currentPage().getParameters().get(TYPE_PARAM),
ApexPages.currentPage().getParameters().get(NAME_PARAM)
);
}
}
VisualForce
This would change your Javascript
specifically to:
var listViewUrl = "{!URLENCODE(listViewUrl)}";
if (listViewUrl) parent.frames.location.replace(listViewUrl);
Best Answer
You can do the same type of redirect using the same markup and the same edit to the Custom Object's "Buttons, Links, and Actions" Tab config.
If you've got a custom object named
Cool_Widget__c
you'll just substitute that API name in place ofAccount
in your example.Then to override the tab behavior, navigate to Setup > Create > Objects, click on the Cool Widget object's name, scroll down to "Buttons, Links, and Actions" and click edit next to the Tab. Choose the VF option, find your page in the list and click save. Done.