Tuesday 12 February 2013

Usage of StandardSetController Class and Methods


StandardSetController Class
===================
-StandardSetController objects allow you to create list controllers similar to, or as extensions of, the pre-built Visualforce list controllers provided by Salesforce.

-This is useful for writing pages that perform mass updates (applying identical changes to fields within a collection of objects).

-Fields that are required in other Salesforce objects will keep the same requiredness when used by the prototype object.

Instantiation
========

-You can instantiate a StandardSetController in either of the following ways:

-From a list of sObjects:
List accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new   ApexPages.StandardSetController(accountList);

-From a query locator:
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,CloseDate FROM Opportunity]));

-Note:
-The maximum record limit for StandardSetController is 10,000 records.
-Instantiating StandardSetController using a query locator returning more than 10,000 records causes a LimitException to be thrown.
-However, instantiating StandardSetController with a list of more than 10,000 records doesn’t throw an exception, and instead truncates the records to the limit.

Methods
=======

-StandardSetController methods are all called by and operate on a particular instance of a StandardSetController.



Example
======

The following example shows how a StandardSetController object can be used in the constructor for a custom list controller:

public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List getOpportunities() {
        return (List) setCon.getRecords();
    }
}


The following Visualforce markup shows how the controller above can be used in a page:

   
   
   
       
           
           
       
   



     

2 comments:

  1. You literally copy pasted this from the salesforce API. unreal

    ReplyDelete
  2. Great piece of writing, I really liked the way you highlighted some really important and significant points. Thanks so much, I appreciate your work.
    Web Development Chicago

    ReplyDelete

Thank you for visiting. Your comments are highly appreciated.