+ Start a Discussion
King KooKing Koo 

Diff between getRecord and getRecords for standardSETController

Hi guys

 

Reading through the VF Developer's Guide.  On page 85 there is this class defined as follows (it's a controller extension class)

 

public with sharing class AccountPagination {
private final Account acct;
// The constructor passes in the standard controller defined // in the markup below public AccountPagination(ApexPages.StandardSetController controller) { this.acct = (Account)controller.getRecord(); }
public ApexPages.StandardSetController accountRecords { get { if(accountRecords == null) { accountRecords = new ApexPages.StandardSetController( Database.getQueryLocator([SELECT Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Opportunity WHERE IsClosed = true)])); } return accountRecords; } private set; }
public List<Account> getAccountPagination() { return (List<Account>) accountRecords.getRecords(); } }

 Can someone please let me know what the constructor does?  

 

For a simple standard controller extension, the constructor looks like this:

 

public myControllerExtension(ApexPages.StandardController stdController) {
this.acct = (Account)stdController.getRecord();
}

 and I have no problem understanding that, you pass the controller to the extension class, and it always involves one record, so you get the record by calling the getRecord() method and cast it to an Account type.

 

However, when it is a standard set controller, I have problem understanding the purpose of calling the getRecord method (as opposed to the getRecords, the plural form).  SInce it is a standard set controller, I would think it returns a list of records. Because of that, the constructor like this,

public AccountPagination(ApexPages.StandardSetController controller) {
this.acct = (Account)controller.getRecord();
}

 

doesn't make sense to me.

 

I would've thought the constructor would look something more like this:

 

public AccountPagination(ApexPages.StandardSetController controller) {
List<Account> accounts = (List<Account>)controller.getRecords();
}

 

i.e. changng from singular to plural and changing the method from getRecord to getRecords.

 

So, my question is, what exactly does getRecord do, and why is the constructor the way it is (in the singular form, and not in the plural form)?

 

Thanks a lot

King

sfdcfoxsfdcfox

If you read the documentation, you would see that "getRecord()" is a "prototype" object. That means you can do this:

Account a = (Account)stdSetCtrlr.getRecord();
a.name='Renamed Account';
stdSetCtrlr.save();

Which will rename ALL accounts referenced by the standard set controller stdSetCtrlr to "Renamed Account." This is useful for mass updates instead of having to update each individual record in a for loop.

 

EditMissed part of the question. The INTENT of the page is to allow you to mass update accounts; therefore, they use the prototype object so that you don't have to use for loops, etc.

King KooKing Koo

Hi there

 

Thanks for your reply.  Yes I saw the documentation saying it's a prototype object - but I don't think I understand it.    When you have this line of code:

 

Account a = (Account)stdSetCtrlr.getRecord();

 what does the variable a hold?  

 

So, I tried to make an example out of what you said.  I have an object called "Small_Set__c" that only has one column, the standard Name field, and 5 records, whose values are A, B, C, D and E.

 

I thought I would list them with <dataList> and then use a <commandButton> to mass update them to Z.  This is my class:

 

public class zyzyz 
{
    private Small_Set__c ss;
    private ApexPages.standardSetController controller;
    
    public zyzyz(ApexPages.standardSetController pcontroller)
    {
        this.controller = pcontroller;
        
    }
    
    public PageReference MassUpdateSmallSet()
    {
        this.ss = (SMall_Set__c)this.controller.getRecord();
        this.ss.name = 'Z';
        this.controller.save();  
        return null; 
    }
    
    public ApexPages.StandardSetController SSRecords
    {
        get
        {
            if (SSRecords == null)
            {
                SSRecords = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name from small_set__c]));
            }
            return SSRecords;
        }
        private set;
    }
    
    public List<small_set__c> getSmall()
    {
        return (List<Small_set__c>)SSRecords.getRecords();
    }
}

 

and this is my VF:

 

<apex:page standardController="small_set__c" recordSetVar="smallsets" extensions="zyzyz">
    <apex:form>
        <apex:pageBlock title="Viewing ss">
            <apex:pageBlockSection>
                <apex:dataList value="{!Small}" var="ss">
                    {!ss.name}
                </apex:dataList>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton value="SAVE" action="{!MassUpdateSmallSet}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

However, when I hit the Save, nothing gets saved.  Can you please let me know what I did wrong?

 

THanks a lot.

King

 

Thanks

King

rushi ettam 7rushi ettam 7
When I reading the VFPages Pdf, the same doubt raised in my mind
after some research I found out these useful definitions for the getRecord() method.

StandardSetController
getRecord(): Returns the sObject that represents the changes to the selected records. This retrieves the prototype object contained within the class  and is used for performing mass updates.
getRecords():Returns the list of sObjects in the current page set. This list is immutable, i.e. you can't call clear() on it.

StandardController:
getRecord():Returns the ID of the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.
public AccountPagination(ApexPages.StandardSetController controller) {
this.acct = (Account)controller.getRecord();
}
So the abvce Example returns the prototype of the SObject not the collection of SObjects
I hope this will clarify your question