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
jdk4702jdk4702 

How to Rerender StandardSetController in PageBlockTable?

How do I rerender a StandardSetController? I've seen these posts indicating that I should requery the query locator, but I can't determine how to trigger the requery upon rerender, as I assumed that would happen automatically upon rerender.

http://community.salesforce.com/t5/Apex-Code-Development/Reload-StandardSetController-Records/m-p/193333

http://community.salesforce.com/t5/Visualforce-Development/Rerender-problem/td-p/173007

 

 

I'm not sure how to "make StandardSetController a property and re-initialize it".

 

My not working code:

 

 

<apex:page controller="testGetter">
<apex:pageblock>
<apex:form>
<APex:pageblocksection>
<apex:selectList value="{!inputBox}" size="1" title="Object List">
<apex:selectOptions value="{!rObjectsInUse}"></apex:selectOptions>
</apex:selectList>
<apex:commandButton rerender="preview" value="click me"> //when clicked, this should rerender the pageblocktable, but doesn't
</apex:commandbutton>
</apex:pageblocksection>
</apex:form>
</apex:pageBlock>

<apex:pageblock>

<apex:pageBlockSection id="preview">
<apex:outputtext value="{!inputbox}"/>
<br/>
<apex:pageBlockTable value="{!sRecords}" var="r"> //Doesn't rerender
<apex:column value="{!r.Id}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>

</apex:page>

 

public with sharing class testGetter {

public String inputBox { get
{
system.debug('input box is: '+inputBox);
if(inputbox==null){
inputbox='Account';
}
return inputBox;
}

set; }

public List<SelectOption> getrObjectsInUse(){
List<SelectOption> options = new List<SelectOption>();
// AggregateResult[] ars=[SELECT objectName__c name1 FROM Replacer__c WHERE objectName__c!=null AND Active__c=True GROUP BY objectName__c];

// if(ars.size()>0){
// for (AggregateResult ar:ars){//for 1
// String objectName=String.valueof(ar.get('name1'));
options.add(new selectOption('Account','Account'));
options.add(new selectOption('Contact','Contact'));
options.add(new selectOption('Lead','Lead'));
// }//for 1
// }//if 1
return options;
}

public String recordQuerySQL(){
String recordQuerySQL = 'SELECT Id FROM '+inputBox;
system.debug('ObjectName :'+inputBox);
//make this query logic dynamic based on:
//1) Object
//2) Fields in the rules
//3) Records that meet the "find" criteria
return recordQuerySQL;
}//recordQuerySQL

public ApexPages.StandardSetController setCon{
get {
if(setCon==null){
String query = recordQuerySQL();
setCon = new ApexPages.StandardSetController(Database.getQueryL ocator(query));//how do I update this query for the set controller upon button click, which rerenders the PageBlockTable?
}
return setCon;
}//get
private set;
}//sRecords
public List<sObject> getsRecords() {
return (List<sObject>) setCon.getRecords();
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
jdk4702jdk4702

I finally figured it out!  As I used the sample code templates for SetCon, there was a condition If (setCon==null){}, which prevented my set controller from re-intializaing if it was not null upon rerender.

 

Here's the fixed code - notice the commented out if:

 

public with sharing class testGetter {

    public String inputBox { get
    {
        system.debug('input box is: '+inputBox);
        if(inputbox==null){
            inputbox='Account';
        }
        return inputBox;
    }
    
     set; }
     
     public List<SelectOption> getrObjectsInUse(){
        List<SelectOption> options = new List<SelectOption>();
        AggregateResult[] ars=[SELECT objectName__c name1 FROM Replacer__c WHERE objectName__c!=null AND Active__c=True GROUP BY objectName__c];
        
        if(ars.size()>0){
            for (AggregateResult ar:ars){//for 1
                String objectName=String.valueof(ar.get('name1'));
                options.add(new selectOption(objectName,objectName));
            }//for 1    
        }//if 1
        return options;
    }
    
    public String recordQuerySQL(){
        String recordQuerySQL = 'SELECT Id FROM '+inputBox;
        system.debug('ObjectName :'+inputBox);
        return recordQuerySQL;
    }//recordQuerySQL
    
    public ApexPages.StandardSetController setCon{
        get {
//            if(setCon==null){
                String query = recordQuerySQL();
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(query));
//            }
            return setCon;
        }//get
        private set;
    }//sRecords

    public List<sObject> getsRecords() {
         return (List<sObject>) setCon.getRecords();
    }   
}

All Answers

jdk4702jdk4702

I finally figured it out!  As I used the sample code templates for SetCon, there was a condition If (setCon==null){}, which prevented my set controller from re-intializaing if it was not null upon rerender.

 

Here's the fixed code - notice the commented out if:

 

public with sharing class testGetter {

    public String inputBox { get
    {
        system.debug('input box is: '+inputBox);
        if(inputbox==null){
            inputbox='Account';
        }
        return inputBox;
    }
    
     set; }
     
     public List<SelectOption> getrObjectsInUse(){
        List<SelectOption> options = new List<SelectOption>();
        AggregateResult[] ars=[SELECT objectName__c name1 FROM Replacer__c WHERE objectName__c!=null AND Active__c=True GROUP BY objectName__c];
        
        if(ars.size()>0){
            for (AggregateResult ar:ars){//for 1
                String objectName=String.valueof(ar.get('name1'));
                options.add(new selectOption(objectName,objectName));
            }//for 1    
        }//if 1
        return options;
    }
    
    public String recordQuerySQL(){
        String recordQuerySQL = 'SELECT Id FROM '+inputBox;
        system.debug('ObjectName :'+inputBox);
        return recordQuerySQL;
    }//recordQuerySQL
    
    public ApexPages.StandardSetController setCon{
        get {
//            if(setCon==null){
                String query = recordQuerySQL();
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(query));
//            }
            return setCon;
        }//get
        private set;
    }//sRecords

    public List<sObject> getsRecords() {
         return (List<sObject>) setCon.getRecords();
    }   
}
This was selected as the best answer
Developer MDeveloper M

The solution only refreshes the records set, but it breaks the page navigations such as next. If you omit the is null check for the setController, the next button won't work. With the null check, all standard set navigations work. Wonder if anyone has solutions.