0

I am trying to learn custom list controllers but struggling to understand how the getter and setter methods execute using standard set controller. Can anyone please help with code comments as to how they execute in order and call the methods?

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();
        }
    } 
2

The actual order depends on what markup is in the page. setCon's get (the getter method) could be called multiple times, and could be called in any order relative to getOpportunities. getOpportunities itself could also be called multiple times. The only thing we know for sure from this code is that getOpportunities will automatically call setCon's get method to initialize the variable if it hasn't already been initialized.

The approximate order of execution is as follows:

public class opportunityList2Con {
    public ApexPages.StandardSetController setCon {
        // Step 3: The getter is called
        get {
            // Step 4: The variable is currently null, so...
            if(setCon == null) {
                // Step 7: The StandardSetController is assigned to the variable
                setCon = 
                // Step 6: The constructor is called with a QueryLocator
                // At this point, the 10,000 row limit is checked, exception thrown if exceeded
                // Internal state of StandardSetController is initialized
                new ApexPages.StandardSetController(
                    // Step 5: A QueryLocator is initialized with the inline query
                    // Note, this is special "magic", the query is not yet fully executed.
                    Database.getQueryLocator(
                        [SELECT Name, CloseDate FROM Opportunity]
                    )
                );
            }
            // Step 8: the value is returned to the caller
            return setCon;
        }
        // Never used in this code
        set;
    }

    // Step 1: {!opportunities} is in the markup
    public List<Opportunity> getOpportunities() {
        // Step 10: The list is returned to the page
        return 
        // Step 9: The list is cast from sObject[] to Opportunity[]
        (List<Opportunity>) 
        // Step 2: The variable sees .getRecords(), so it must call getter
        setCon
        // Step 8: The method is called on the value returned from the getter
        .getRecords();
    }
} 

Note that if other expressions are in play, such as {!setCon.hasNext}, this order may be different, and may even be randomly evaluated on each page load. The system makes no guarantee about the order in which getters are called with regards to expressions that are in the Visualforce markup.

3
  • Thank you for the excellent answer with all the comments. I have one doubt here: why is the constructor called public ApexPaves.StandardSetController setCon? I don’t understand this line at all as to what it actually does. Can you please help to explain? – Krishnamurthy Dec 28 '20 at 13:09
  • @Krishnamurthy It's not a constructor. It's a variable (technically a property) that has a custom getter and default setter. public is the access level, ApexPages.StandardController is the data type, and setCon is the property name. – sfdcfox Dec 28 '20 at 14:58
  • Got it. Thank you. – Krishnamurthy Dec 28 '20 at 15:17

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.