0

I want to know how StandardSetController actually works. My expectation on the insight is to understand how salesforce is handling the result set, in contrary with normal re-querying of records with offset and limits. How does the controller behave differently/better compare to a custom class implementation?

3

The StandardSetController uses a database cursor to paginate through the results. Unlike using LIMIT+OFFSET, this method supports 10,000 rows of data, the results will not change each time a new page is pulled back from the database (we call this a "consistent view" of the data), no custom code is required to implement paginating, page sizing, and saving modifications, and you can also mass edit records without custom code. However, being a cursor also means that it only remains viable for 15 minutes, after which a new query must be issued.

StandardSetController is typically better for naive paging implementations, since it requires the least amount of code and is trivial to use. The most powerful implementation of pagination I've ever written supported ~50,000 rows, an unlimited timeout, and a semi-consistent view (the rows were consistent but the field values were not), but did require ~100 lines of code, so it was fairly non-trivial in scope. Client-side pagination using remote actions or an API can support millions of rows with a stable view, if desired, but requires more memory and a rather significant amount of code. The StandardSetController offers the most functionality with the least amount of code needed to get up and running.

1

StandardController

From the docs :

A Visualforce controller is a set of instructions that specify what happens when a user interacts with the components specified in associated Visualforce markup, such as when a user clicks a button or link. Controllers also provide access to the data that should be displayed in a page, and can modify component behavior.

You can find more information here, but basically a StandardController is for ONE record, i.e. if you'd like to create a new Visualforce page for a single record, you'd use a Standard Controller in your Apex.

StandardSetController

From the docs :

Standard list controllers allow you to create Visualforce pages that can display or act on a set of records. Examples of existing Salesforce pages that work with a set of records include list pages, related lists, and mass action pages.

You can find more information here, but basically Set (list) controllers are for MULTIPLE (or a list of) records, i.e. if you'd like to create a new Visualforce page for a List of records (or even from a selection of records on a List view), you'd use a Standard Set Controller in your Apex.

Example of StandardSetController

Apex Class

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<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    }
}

Visualforce Page

<apex:page controller="opportunityList2Con">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.CloseDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

For more information click here : StandardSetController

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.