You need to sign in to do that
Don't have an account?
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
If you read the documentation, you would see that "getRecord()" is a "prototype" object. That means you can do this:
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.
Edit: Missed 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.
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:
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:
and this is my VF:
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
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