[SalesForce] Override default “Recent Order” list view

I have been using a visualforce tab to override the default "Recent Order" list view for a custom object "orders__c"

Following is my code…

 <apex:page >
    <script type="text/javascript">
      parent.frames.location.replace("/a0q?fcf=00BK0000000yabc");                                          
 </script>
</apex:page>

I face problem everytime I migrate because of hardcoding the list-view id.

Is there a better or out of the box solution? Thanks in advance.

EDIT: I also came across this great hack by deepak. But then again the hardcoding issue arises.

Best Answer

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);
Related Topic