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
divya manohardivya manohar 

select criteria in standard set controller

Hello
I have a requirement wherein I need to have a custom button (Fetch records) in Account object List View, which will show only Name,Phone and Industry=consulting.

My vf page is working and I have added the button to list view and when I select AllAcounts it shows the vf page with records.
But I need only those records where Industry=consulting and here I am getting all the records.
Is my approach right ? pls guide me
public class AccountExt
{
    //List<Account> acctList{get;set;}
    public AccountExt(ApexPages.StandardSetController controller) { }
    
    public List<Account> accountrec()
    {
       List<Account> acctList=[select Name,Phone,Industry from Account where Industry='Consulting'];

       ApexPages.StandardSetController ssc=new ApexPages.StandardSetController(acctList); //Instatiate standardsetcontroller

       return ssc.getRecords(); //getRecords method of StandardSetController
    }
}

<apex:page standardController="Account"
           extensions="AccountExt"
           recordSetVar="Accounts"
           tabStyle="Account"
           sidebar="false">
          
<apex:pageBlock title="Account">

 <apex:pageBlockTable value="{!Accounts}"
                     var="a">
 
 <apex:column value="{!a.Name}" 
              headerValue="Name"/>
              
 <apex:column value="{!a.Phone}" 
              headerValue="Phone"/>
              
 <apex:column value="{!a.Industry}" 
              headerValue="Industry"/>
 </apex:pageBlockTable>
 </apex:pageBlock>
</apex:page>
divya
 
Best Answer chosen by divya manohar
@Karanraj@Karanraj
You are not using the "accountRec" variable in the visualforce page. Here is the updated code

Apex controller 
public class AccountExt
{
    //List<Account> acctList{get;set;}
    public AccountExt(ApexPages.StandardSetController controller) { }
    
    public List<Account> getaccountrec()
    {
       List<Account> acctList=[select Name,Phone,Industry from Account where Industry='Consulting'];

       ApexPages.StandardSetController ssc=new ApexPages.StandardSetController(acctList); //Instatiate standardsetcontroller

       return ssc.getRecords(); //getRecords method of StandardSetController
    }
}

Visualforce page
 
<apex:page standardController="Account"
           extensions="AccountExt"
           recordSetVar="Accounts"
           tabStyle="Account"
           sidebar="false">
          
<apex:pageBlock title="Account">

 <apex:pageBlockTable value="{!accountrec}"
                     var="a">
 
 <apex:column value="{!a.Name}" 
              headerValue="Name"/>
              
 <apex:column value="{!a.Phone}" 
              headerValue="Phone"/>
              
 <apex:column value="{!a.Industry}" 
              headerValue="Industry"/>
 </apex:pageBlockTable>
 </apex:pageBlock>
</apex:page>

 

All Answers

@Karanraj@Karanraj
You are not using the "accountRec" variable in the visualforce page. Here is the updated code

Apex controller 
public class AccountExt
{
    //List<Account> acctList{get;set;}
    public AccountExt(ApexPages.StandardSetController controller) { }
    
    public List<Account> getaccountrec()
    {
       List<Account> acctList=[select Name,Phone,Industry from Account where Industry='Consulting'];

       ApexPages.StandardSetController ssc=new ApexPages.StandardSetController(acctList); //Instatiate standardsetcontroller

       return ssc.getRecords(); //getRecords method of StandardSetController
    }
}

Visualforce page
 
<apex:page standardController="Account"
           extensions="AccountExt"
           recordSetVar="Accounts"
           tabStyle="Account"
           sidebar="false">
          
<apex:pageBlock title="Account">

 <apex:pageBlockTable value="{!accountrec}"
                     var="a">
 
 <apex:column value="{!a.Name}" 
              headerValue="Name"/>
              
 <apex:column value="{!a.Phone}" 
              headerValue="Phone"/>
              
 <apex:column value="{!a.Industry}" 
              headerValue="Industry"/>
 </apex:pageBlockTable>
 </apex:pageBlock>
</apex:page>

 
This was selected as the best answer
divya manohardivya manohar
Thanks