function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
George thomasGeorge thomas 

Standardset controller pagination setSelectedRecord() clears out during pagination

Hi All,

I'm trying to utilze the standard set controller setSelectedRecord() function to capture the selected records from the screen while paginating from one page to another. But when i add the records from the 2nd page into the standardsetcontroller variable the records which i have already added to it become lost.

Any idea why its happening so?
 
VF Page:
<apex:page standardController="Account" Extensions="accountCon" >
<apex:form id="form1" >
<apex:pageBlock >
<apex:pagebLockTable value="{!accList}" var="a">
    <apex:column >
        <apex:inputCheckbox value="{!a.selected}" onclick="updateSel()"/>
    </apex:column>
    <apex:column value="{!a.Acc['Name']}"/>
    
    
</apex:pagebLockTable>  

<apex:commandButton action="{!next}" value="Next"/>


<apex:pageBlockTable value="{!selAcc}" var="acc" id="pgblock2">

    <apex:column value="{!acc['Name']}"/>
</apex:pageBlockTable>

<apex:actionFunction name="updateSel" action="{!updateSel}" reRender="pgblock2"/>

</apex:pageBlock>
</apex:form>
</apex:page>


Controller:

public class accountCon {

public list<Sobject> selAcc{get{
    selAcc = selAcc();
    return selAcc;
}set;}
public list<accountWrapper> accList{get
{
    return accLIst;   
}

set;}
    public accountCon(ApexPages.StandardController controller) {
    selAcc = new List<Sobject>();
    getAccounts();
    }

ApexPages.standardSetController sCon;


public List<accountWrapper> getAccounts()
{
    if(sCon==null)
        sCon = new ApexPages.standardSetController(Database.getQueryLocator([select id,name,type from Account]));
    sCon.setPageSize(5);
    
    if(accLIst == null)
        accLIst = new List<accountWrapper>();
        
    for(sobject s:sCon.getRecords())
    {
        accountWrapper wrp = new accountWrapper();
        wrp.selected = false;
        wrp.acc=s;
        accLIst.add(wrp);
    }
    return accList;
}

public void updateSel()
{
    List<Sobject> s = new List<Sobject>();
    for(accountWrapper awrp :accList )
    {
        if(awrp.selected)
         s.add(awrp.acc);
    }
    system.debug('Before set selected:'+scon.getSelected());
    sCon.setSelected(s);
    system.debug('After set selected:'+scon.getSelected());
}


public list<Sobject> selAcc()
{
    return scon.getSelected();
}
public void next()
{
    accList .clear();
    sCon.Next();
    accList  = getAccounts();
}

class accountWrapper
{
    public Boolean selected{get;set;}
    public Sobject Acc{get;set;}
}
}

 
ProlayProlay
Do not use the ​Database.getQueryLocator. Instead use the create SOQL query and query the database directly.
George thomasGeorge thomas
Thanks for looking into this. It's not working even i use SOQL directly or Database.query(queryString).