[SalesForce] custom list view that allows new filter parameters

I'm looking to create a VF page with a list of records and dynamic filter criteria. The user needs to be able to filter the records based on various criteria and also save those filters for future use. They also need to be able to select certain records via checkboxes and perform an action (creating other records).

Before I launch into a full custom page would there be any advantage to leveraging Standard Set Controllers or any of the list view apex tags to support this? I'm not performing a bulk update to these records I'm just creating new ones based on them so I'm not sure Standard Set Controllers are that helpful but I've just learned about them.

Also, could I leverage the Objects list views as filter criteria and programatically create new list views?

Thanks!

Best Answer

Yes you can leverage user defined List Views programatically via the StandardSetController. In two ways, one is utilising the one the platform creates for you or by creating an instance of this class yourself.

Take a look at these questions and answers for examples StandardSetController List Instatiation and setFilterId. Also this one around Custom Buttons that can sit on the standard List View UI and provide a check box UI for free, which is the most common way to use them Creating a custom list-view button that handles multi-record selection.

If you need more flexibility, as mentioned in one of the referenced questions above, you can choose to create your own instance, it's quite an advanced usage though, so consider the Custom Button route first. Here is a code example where you can set the List View (via setFilterId) and effectively reuse the criteria the user has setup using the standard List View UI. A great example of going native on the platform!

string SOQL = 'SELECT Id, Name, Phone FROM Account Order By Name DESC';
List<Account> accounts = Database.query(SOQL);
for(Account account : accounts)
    System.debug(account.Name + ', Phone: ' + account.Phone);
ApexPages.StandardSetController ssc = 
     new ApexPages.StandardSetController(Database.getquerylocator(SOQL)); 
ssc.setPageSize(2000);
ssc.setFilterId('00BG000000702tj'); // 'Platinum and Gold SLA Customers'
ssc.setPageNumber(1);
for(Account account : (List<Account>) ssc.getRecords())
    System.debug(account.Name + ', Phone: ' + account.Phone);

Note: You can enumerate the List View's names and Id's to present a choice to the user or find one you've packaged via getListViewOptions().

Hope this helps, give you some food for thought!