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
satakshisatakshi 

How to apply filter on city in the custom button

Hello,

 I have created custom button on custom object. In this custom button i want to select records according to particular city. When i will selcet city "Mumbai" then i have to get all accounts having city mumbai. So that i can select some records from mumbai only. I have created code to display all the accounts records and to show selected some and its working fine. I want to know that how to apply filter so that i will get records according selected city.


Controller:
public with sharing class MyCompController1


public String str;
    public MyCompController1(ApexPages.StandardController controller) {
    
   str= ApexPages.currentPage().getParameters().get('id');
   system.debug('**************Record id is**********************'+str);
   if(wrapAccountList == null)
{
wrapAccountList = new List<wrapAccount>();
for(Account a: [select Id, Name, BillingCity from Account])
{
wrapAccountList.add(new wrapAccount(a));
system.debug('*****************wrapAccountList******************'+wrapAccountList);
}
}

    }



public List<wrapAccount> wrapAccountList {get; set;}
public List<Account> selectedAccount{get;set;}

private List<Id> accountids=new list<Id>();
public List<Account> acc;

public PageReference processSelected()
{
    selectedAccount = new List<Account>();

    for(wrapAccount wrapAccountObj : wrapAccountList)
    {
    if(wrapAccountObj.selected == true)
    {
        selectedAccount.add(wrapAccountObj.acc);
        //accWrap.acc.Name
        system.debug('*****************!!!!!!!!!wrapAccountList******************'+selectedAccount);

    }
    }
 /*for(Account acc: selectedAccounts)
{
Daily_Visit_Plan__c pv =new Daily_Visit_Plan__c();
pv.Name__c= acc.id;
insert pv;


} */
            PageReference pageRef = new PageReference('/apex/Visitplan');
           
            return pageRef;
}


public list<Account> srecs;
public list<Account> getSrecs()
{
 srecs= [select Id, Name, BillingCity from Account];
 system.debug('*****************!!!!!!!!!srecssrecssrecssrecs******************'+srecs);

        return selectedAccount;
}

public class wrapAccount {
public Account acc {get; set;}
public Boolean selected {get; set;}

public wrapAccount(Account a)
 {
acc = a;
selected = false;
}
}
}




VF PAGE1:


<apex:page standardController="Account" extensions="MyCompController1" standardStylesheets="false" sidebar="false">
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID)
{
var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++)
{
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1)
{
inputCheckBox[i].checked = obj.checked;
} } }
</script>
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Create Visit" action="{!processSelected}" />
</apex:pageBlockButtons>
<apex:pageblockSection title="All Contacts" collapsible="false" columns="1">
<apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" title="All Doctors And Chemists">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
</apex:column>
<apex:column value="{!accWrap.acc.Name}" />
<apex:column value="{!accWrap.acc.BillingCity}" />
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

VF PAGE 2:
<apex:page standardController="Account" extensions="MyCompController1" sidebar="false">
<apex:pageBlock >
<apex:pageBlockSection title="Displaying selected records.." collapsible="false">
<apex:pageBlockTable value="{!srecs}" var="item" >
<apex:column value="{!item.id}"/> <apex:column value="{!item.name}"/>
<apex:column value="{!item.BillingCity}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
 
Best Answer chosen by satakshi
Ankur Saini 9Ankur Saini 9
Hi 
if u have picklist of city, here i am taking picklist through code
try this:
 
<apex:page standardController="Account" extensions="MyCompController1" standardStylesheets="false" sidebar="false">
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID)
{
var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++)
{
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1)
{
inputCheckBox[i].checked = obj.checked;
} } }
</script>
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Create Visit" action="{!processSelected}" />
<apex:commandButton value="Show" action="{!dosome}" reRender="table"/>
</apex:pageBlockButtons>
<apex:actionRegion >

<apex:selectList value="{!city}" size="1">
            <apex:selectOptions value="{!items}"/>
            <!--<apex:actionSupport event="onchange" reRender="table" action="{!dosome}" />-->
        </apex:selectList>
        </apex:actionRegion>
        

<apex:pageblockSection title="All Contacts" collapsible="false" columns="1">
<apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" title="All Doctors And Chemists" id="table">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
</apex:column>
<apex:column value="{!accWrap.acc.Name}" />
<apex:column value="{!accWrap.acc.BillingCity}" />
</apex:pageBlockTable>
</apex:pageblockSection>

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



public with sharing class MyCompController1
{ 

public String str;
public String city{get;set;}
    public MyCompController1(ApexPages.StandardController controller) {
    
   str= ApexPages.currentPage().getParameters().get('id');
   system.debug('**************Record id is**********************'+str);
   /*if(wrapAccountList == null)
{
wrapAccountList = new List<wrapAccount>();
for(Account a: [select Id, Name, BillingCity from Account where BillingCity='Mumbai'])
{
wrapAccountList.add(new wrapAccount(a));
system.debug('*****************wrapAccountList******************'+wrapAccountList);
}
}*/

    }
    
    public pagereference dosome(){
    
wrapAccountList = new List<wrapAccount>();
for(Account a: [select Id, Name, BillingCity from Account where BillingCity=:city])
{
wrapAccountList.add(new wrapAccount(a));
system.debug('*****************wrapAccountList******************'+wrapAccountList);
}

    return null;
    }

public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('--None--','--None--'));
            options.add(new SelectOption('Mumbai','Mumbai'));
            options.add(new SelectOption('Delhi','Delhi'));
            return options;
        }

public List<wrapAccount> wrapAccountList {get; set;}
public List<Account> selectedAccount{get;set;}

private List<Id> accountids=new list<Id>();
public List<Account> acc;

public PageReference processSelected()
{
    selectedAccount = new List<Account>();

    for(wrapAccount wrapAccountObj : wrapAccountList)
    {
    if(wrapAccountObj.selected == true)
    {
        selectedAccount.add(wrapAccountObj.acc);
        //accWrap.acc.Name
        system.debug('*****************!!!!!!!!!wrapAccountList******************'+selectedAccount);

    }
    }
 /*for(Account acc: selectedAccounts)
{
Daily_Visit_Plan__c pv =new Daily_Visit_Plan__c();
pv.Name__c= acc.id;
insert pv;


} */
            PageReference pageRef = new PageReference('/apex/Accountvf2');
           
            return pageRef;
            
}


public list<Account> srecs;
public list<Account> getSrecs()
{
 srecs= [select Id, Name, BillingCity from Account];
 system.debug('*****************!!!!!!!!!srecssrecssrecssrecs******************'+srecs);

        return selectedAccount;
}

public class wrapAccount {
public Account acc {get; set;}
public Boolean selected {get; set;}

public wrapAccount(Account a)
 {
acc = a;
selected = false;
}
}
}

Thanks
Ankur Saini
http://mirketa.com
 

All Answers

Ankur Saini 9Ankur Saini 9
Hi 
if u have picklist of city, here i am taking picklist through code
try this:
 
<apex:page standardController="Account" extensions="MyCompController1" standardStylesheets="false" sidebar="false">
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID)
{
var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++)
{
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1)
{
inputCheckBox[i].checked = obj.checked;
} } }
</script>
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Create Visit" action="{!processSelected}" />
<apex:commandButton value="Show" action="{!dosome}" reRender="table"/>
</apex:pageBlockButtons>
<apex:actionRegion >

<apex:selectList value="{!city}" size="1">
            <apex:selectOptions value="{!items}"/>
            <!--<apex:actionSupport event="onchange" reRender="table" action="{!dosome}" />-->
        </apex:selectList>
        </apex:actionRegion>
        

<apex:pageblockSection title="All Contacts" collapsible="false" columns="1">
<apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" title="All Doctors And Chemists" id="table">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
</apex:column>
<apex:column value="{!accWrap.acc.Name}" />
<apex:column value="{!accWrap.acc.BillingCity}" />
</apex:pageBlockTable>
</apex:pageblockSection>

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



public with sharing class MyCompController1
{ 

public String str;
public String city{get;set;}
    public MyCompController1(ApexPages.StandardController controller) {
    
   str= ApexPages.currentPage().getParameters().get('id');
   system.debug('**************Record id is**********************'+str);
   /*if(wrapAccountList == null)
{
wrapAccountList = new List<wrapAccount>();
for(Account a: [select Id, Name, BillingCity from Account where BillingCity='Mumbai'])
{
wrapAccountList.add(new wrapAccount(a));
system.debug('*****************wrapAccountList******************'+wrapAccountList);
}
}*/

    }
    
    public pagereference dosome(){
    
wrapAccountList = new List<wrapAccount>();
for(Account a: [select Id, Name, BillingCity from Account where BillingCity=:city])
{
wrapAccountList.add(new wrapAccount(a));
system.debug('*****************wrapAccountList******************'+wrapAccountList);
}

    return null;
    }

public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('--None--','--None--'));
            options.add(new SelectOption('Mumbai','Mumbai'));
            options.add(new SelectOption('Delhi','Delhi'));
            return options;
        }

public List<wrapAccount> wrapAccountList {get; set;}
public List<Account> selectedAccount{get;set;}

private List<Id> accountids=new list<Id>();
public List<Account> acc;

public PageReference processSelected()
{
    selectedAccount = new List<Account>();

    for(wrapAccount wrapAccountObj : wrapAccountList)
    {
    if(wrapAccountObj.selected == true)
    {
        selectedAccount.add(wrapAccountObj.acc);
        //accWrap.acc.Name
        system.debug('*****************!!!!!!!!!wrapAccountList******************'+selectedAccount);

    }
    }
 /*for(Account acc: selectedAccounts)
{
Daily_Visit_Plan__c pv =new Daily_Visit_Plan__c();
pv.Name__c= acc.id;
insert pv;


} */
            PageReference pageRef = new PageReference('/apex/Accountvf2');
           
            return pageRef;
            
}


public list<Account> srecs;
public list<Account> getSrecs()
{
 srecs= [select Id, Name, BillingCity from Account];
 system.debug('*****************!!!!!!!!!srecssrecssrecssrecs******************'+srecs);

        return selectedAccount;
}

public class wrapAccount {
public Account acc {get; set;}
public Boolean selected {get; set;}

public wrapAccount(Account a)
 {
acc = a;
selected = false;
}
}
}

Thanks
Ankur Saini
http://mirketa.com
 
This was selected as the best answer
satakshisatakshi
Hello Ankur,

   Its exactly what i want. You explain it very well. Your code is understandable. Thanks for the help.

Thanks & Best Regards,
Satakshi