Activity Forums Salesforce® Discussions StandardSetController in salesforce

  • chanchal kumar

    Member
    September 3, 2018 at 1:19 pm

    Hello Shradha,

    StandardSetController objects allow you to create list controllers similar to, or as extensions of, the pre-built Visualforce list controllers provided by Salesforce.
    The StandardSetController class also contains a prototype object. This is a single sObject contained within the Visualforce StandardSetController class. If the prototype object's fields are set, those values are used during the save action, meaning that the values are applied to every record in the set controller's collection. This is useful for writing pages that perform mass updates (applying identical changes to fields within a collection of objects).

  • madhulika shah

    Member
    September 3, 2018 at 1:27 pm

    Hello Shradha,

    ApexPages.StandardSetController (documentation) contains a list of records (one or more), and has additional functions to facilitate pagination (moving between pages) and updating a number of records at once.

    List<account> accountList = [SELECT Name FROM Account LIMIT 20];
    ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);

  • Avnish Yadav

    Member
    September 3, 2018 at 1:37 pm

    Hello,

    StandardSetController allows us to create list controllers similar to, or as extensions of, the pre-built Visualforce list controllers provided by Salesforce.

    For e.g-  when you want to override a tab or list view with your custom visualforce page like:-

    Page:-

    <apex:page standardController="Account" recordsetvar="Accounts extension="AccListController">

    <apex:pageBlock>
    <apex:pageBlockTable value="{!accountList}" var="acc">
    <apex:column value="{!acc.Name}"/>
    <apex:column value="{!acc.Amount}"/>
    </apex:pageBlockTable>
    </apex:pageBlock>

    </apex:page>

     

    Controller

    public class AccListController{
    // 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, Amount FROM Account]));
    }
    return setCon;
    }
    set;
    }

    // Initialize setCon and return a list of records
    public List<Account> getAccountList() {
    return (List<Account>) setCon.getRecords();
    }
    }

    Thanks.

  • shariq

    Member
    September 19, 2018 at 12:09 am

    Hi,

    Another type of example is like this

     

    Page

    <apex:page standardController="Account" recordsetvar="Accounts extension="AccListController">

    <apex:pageBlock>
    <apex:pageBlockTable value="{!accountList}" var="o">
    <apex:column value="{!o.Name}"/>
    <apex:column value="{!o.Amount}"/>
    </apex:pageBlockTable>
    </apex:pageBlock>

    </apex:page>

     

    Controller

    public class AccListController{
    public List<Account> accountList{get;set;}
    public AccListController(ApexPages.StandardSetController setCon) {
    accountList = new List<Account>();
    Map<Id,SObject> accMap = new Map<Id, SObject>(setCon.getSelected());
    accountList = [SELECT ID, Name FROM Account where ID IN :accMap.keyset()];
    System.debug('###setCon.getrecords()###'+setCon.getrecords());
    }
    }

     

    To test this you have to create a Button from which you call your Visualforce page in the List view of Account and select the accounts you want to get and call that button.

     

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs