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
HoysalaHoysala 

Hi guys, i am having a doubt, if i select a picklist, it has to display related accounts of that picklist value... please help me on thsis.. thanks

Khan AnasKhan Anas (Salesforce Developers) 
Hi Suraj,

Greetings to you!

Please try the below code. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="AccBasedOnPicklistC">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:inputField value="{!acc.Rating}" >
                <apex:actionSupport event="onchange" action="{!showAcc}" reRender="pbt"/>
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!accounts}" var="a" id="pbt">
            	<apex:column value="{!a.Name}" />
                <apex:column value="{!a.Rating}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class AccBasedOnPicklistC {

    public Account acc {get;set;}
    public List<Account> accounts {get;set;}
    
    public AccBasedOnPicklistC() {
        acc = new Account();
		accounts = new List<Account>();
    }
    
    public void showAcc() {
        if(acc.Rating == 'Hot'){
            accounts = [SELECT Id, Name, Rating FROM Account WHERE Rating='Hot'];
        }
        if(acc.Rating == 'Warm'){
            accounts = [SELECT Id, Name, Rating FROM Account WHERE Rating='Warm'];
        }
        if(acc.Rating == 'Cold'){
            accounts = [SELECT Id, Name, Rating FROM Account WHERE Rating='Cold'];
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
HoysalaHoysala
Hi Khan Anas, its not working. I have Type picklist, if click on any picklist value, it must show those picklist related accounts... please help
Khan AnasKhan Anas (Salesforce Developers) 
Please share your code.
HoysalaHoysala
public class AccController {
 
    
    public list <account> acc {get;set;} 
    public string searchstring {get;set;} 
    Public Boolean ShowCreateBlock{get;set;}
    public Boolean ShowBlock{get;set;}
    Public Boolean DisplaySearchResults{get;set;}
    public Account account{get;set;}
    Public Account NewAccountRecord{get;set;}
    
    Public Integer noOfRecords {get;set;}
    Public Integer size {get;set;}
    
    public AccController() {
        ShowCreateBlock = false;
        ShowBlock=True;
        DisplaySearchResults = false;
        NewAccountRecord= New Account(Name='Test');  
        size = 10;
    }
    
    public ApexPages.StandardSetController ssc{
        get{
            if(ssc == null){
                String queryResult = 'SELECT Id, Name, Phone, Email__c FROM Account';
                ssc = new ApexPages.StandardSetController(Database.getQueryLocator(queryResult));
                ssc.setPageSize(size);
                noOfRecords = ssc.getResultSize();
            }
            return ssc;
        }
        set;
    }
    
    public String getAccount() {
        return null;
    }
    
    
    public String options { get; set; }
    
    public PageReference executeSearch() {
        return null;
    }
    
    //public list<account> showList = new list<account>([select Name, Email__c,Phone from Account]);
    
    Public list<account> getshowList() {
        return (List<Account>)ssc.getRecords();
    }
    
    public PageReference create() {
        return null;
    }
    
    public pagereference search() { 
        string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20'; 
        acc= Database.query(searchquery);
        DisplaySearchResults =true;
        if(acc.size()==0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'account not found'));
            ShowCreateBlock = true; 
            ShowBlock=False;
        }
        return null;
    }
    
    public void clear() { 
        acc.clear(); 
    } 
    
    Public pagereference saveRecord() {
        acc=[select id,email__c from Account];
        for(Account a : acc) {
            if(NewAccountRecord.Email__c==a.Email__C) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Email is already exist'));
                return null;
            }
        }
        if(ApexPages.hasMessages()==false) {
            insert NewAccountRecord;
            pagereference  pageRef = new pagereference('/'+NewAccountRecord.Id);
            return pageRef ; 
        } 
        return null; 
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('10','10'));
        options.add(new SelectOption('20','20'));
        options.add(new SelectOption('30','30'));
        options.add(new SelectOption('40','40'));
        return options;
    }
 
<apex:page controller="AccController" showHeader="true" tabStyle="account" >
    <apex:form > 
        <apex:pageMessages ></apex:pageMessages>
        <apex:pageblock title="New Account" id="CreateRecordBlock" Rendered="{!ShowCreateBlock}">
            <apex:pageblockSection >
                <apex:inputField value="{!NewAccountRecord.Name}"/>
                <apex:inputField value="{!NewAccountRecord.Phone}"/>  
                <apex:inputField value="{!NewAccountRecord.Email__c}"/>  
            </apex:pageblockSection>
            <apex:pageblockButtons >
                <apex:commandButton value="saveAccount" action="{!saveRecord}"/>
            </apex:pageblockButtons>
        </apex:pageblock>
        
        <apex:pageBlock id="SearchBlock" Rendered="{!ShowBlock}">
            <apex:inputText value="{!searchstring}" label="Input"/> 
            <apex:commandButton value="Search records" action="{!search}" /> 
            <apex:commandButton value="Clear records" action="{!clear}"/> 
            <!-- <apex:commandButton value="Create account" action="{!create}"/>-->
            <apex:pageBlock title="Search Result" id="resBlock" rendered="{!DisplaySearchResults}">
                <apex:actionSupport event="onchange" reRender="Account"/> 
                <apex:pageblockTable value="{!acc}" var="a"> 
                    <apex:column value="{!a.name}"/> 
                    <apex:column value="{!a.id}"/>
                </apex:pageblockTable>
            </apex:pageBlock>
            
            
            <apex:pageBlock id="pb">
                <apex:inputField value="{!Account.Type}"/>
                <apex:pageBlockTable value="{!showList}" var="a" id="pt">
                    <apex:column headerValue="Account Name">
                    
                        <apex:outputLink value="/{!a.id}">{!a.name}</apex:outputLink>
                    </apex:column>
                    <apex:column headerValue="Account Phone" value="{!a.Phone}"/>
                    <apex:column headerValue="Account Email" value="{!a.Email__c}"/>
                    
                    <apex:CommandButton action="{!executeSearch}" value="Search"/>
                    
                    <apex:selectList >
                        <apex:selectOptions value="{!options}"/>
                    </apex:selectList>
                </apex:pageBlockTable>
                
                
                <apex:commandButton value="First" action="{!ssc.first}" disabled="{!!ssc.hasPrevious}" reRender="pt,pb" />
                <apex:commandButton value="Previous" action="{!ssc.previous}" disabled="{!!ssc.hasPrevious}" reRender="pt,pb" />
                <apex:commandButton value="Next" action="{!ssc.next}" disabled="{!!ssc.hasNext}" reRender="pt,pb" />
                <apex:commandButton value="End" action="{!ssc.last}" disabled="{!!ssc.hasNext}" reRender="pt,pb" />
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
HoysalaHoysala
Hi Anas Please say the code, i am not able to achieve it
Khan AnasKhan Anas (Salesforce Developers) 
Hi Suraj,

Please try the below code.

Visualforce:
<apex:page controller="AccController" showHeader="true" tabStyle="account" >
    <apex:form > 
        <apex:pageMessages ></apex:pageMessages>
        <apex:pageblock title="New Account" id="CreateRecordBlock" Rendered="{!ShowCreateBlock}">
            <apex:pageblockSection >
                <apex:inputField value="{!NewAccountRecord.Name}"/>
                <apex:inputField value="{!NewAccountRecord.Phone}"/>  
                <apex:inputField value="{!NewAccountRecord.Email__c}"/>  
            </apex:pageblockSection>
            <apex:pageblockButtons >
                <apex:commandButton value="saveAccount" action="{!saveRecord}"/>
            </apex:pageblockButtons>
        </apex:pageblock>
        
        <apex:pageBlock id="SearchBlock" Rendered="{!ShowBlock}">
            <apex:inputText value="{!searchstring}" label="Input"/> 
            <apex:commandButton value="Search records" action="{!search}" /> 
            <apex:commandButton value="Clear records" action="{!clear}"/> 
            <!-- <apex:commandButton value="Create account" action="{!create}"/>-->
            <apex:pageBlock title="Search Result" id="resBlock" rendered="{!DisplaySearchResults}">
                <apex:actionSupport event="onchange" reRender="Account"/> 
                <apex:pageblockTable value="{!acc}" var="a"> 
                    <apex:column value="{!a.name}"/> 
                    <apex:column value="{!a.id}"/>
                </apex:pageblockTable>
            </apex:pageBlock>
            
            
            <apex:pageBlock id="pb">
                <apex:inputField value="{!Account.Type}">
                    <apex:actionSupport event="onchange" action="{!showAcc}" reRender="pt"/>
                </apex:inputField>
                <apex:pageBlockTable value="{!showList}" var="a" id="pt">
                    <apex:column headerValue="Account Name">
                        
                        <apex:outputLink value="/{!a.id}">{!a.name}</apex:outputLink>
                    </apex:column>
                    <apex:column headerValue="Account Phone" value="{!a.Phone}"/>
                    <apex:column headerValue="Account Email" value="{!a.Email__c}"/>
                    
                    <apex:CommandButton action="{!executeSearch}" value="Search"/>
                    
                    <apex:selectList >
                        <apex:selectOptions value="{!options}"/>
                    </apex:selectList>
                </apex:pageBlockTable>
                
                
                <apex:commandButton value="First" action="{!ssc.first}" disabled="{!!ssc.hasPrevious}" reRender="pt,pb" />
                <apex:commandButton value="Previous" action="{!ssc.previous}" disabled="{!!ssc.hasPrevious}" reRender="pt,pb" />
                <apex:commandButton value="Next" action="{!ssc.next}" disabled="{!!ssc.hasNext}" reRender="pt,pb" />
                <apex:commandButton value="End" action="{!ssc.last}" disabled="{!!ssc.hasNext}" reRender="pt,pb" />
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class AccController {
    
    public list <account> acc {get;set;} 
    public string searchstring {get;set;} 
    Public Boolean ShowCreateBlock{get;set;}
    public Boolean ShowBlock{get;set;}
    Public Boolean DisplaySearchResults{get;set;}
    public Account account{get;set;}
    Public Account NewAccountRecord{get;set;}
    
    Public Integer noOfRecords {get;set;}
    Public Integer size {get;set;}
    
    
    public AccController() {
        ShowCreateBlock = false;
        ShowBlock=True;
        DisplaySearchResults = false;
        NewAccountRecord= New Account(Name='Test');  
        size = 10;
        account = new Account();
    }
    
    public ApexPages.StandardSetController ssc{
        get{
            if(ssc == null){
                String queryResult = 'SELECT Id, Name, Phone, Type, Email__c FROM Account';
                ssc = new ApexPages.StandardSetController(Database.getQueryLocator(queryResult));
                ssc.setPageSize(size);
                noOfRecords = ssc.getResultSize();
            }
            return ssc;
        }
        set;
    }
    
    public void showAcc() {
        if(account.Type == 'Prospect'){
            ssc = new ApexPages.StandardSetController([SELECT Id, Name, Phone, Email__c, Type FROM Account WHERE Type='Prospect']);
            ssc.setPageSize(10);
        }
        if(account.Type == 'Customer - Direct'){
            ssc = new ApexPages.StandardSetController([SELECT Id, Name, Phone, Email__c, Type FROM Account WHERE Type='Customer - Direct']);
        	ssc.setPageSize(10);
        }
        if(account.Type == 'Customer - Channel'){
            ssc = new ApexPages.StandardSetController([SELECT Id, Name, Phone, Email__c, Type FROM Account WHERE Type='Customer - Channel']);
        	ssc.setPageSize(10);
        }
        if(account.Type == 'Channel Partner / Reseller'){
            ssc = new ApexPages.StandardSetController([SELECT Id, Name, Phone, Email__c, Type FROM Account WHERE Type='Channel Partner / Reseller']);
        	ssc.setPageSize(10);
        }
        if(account.Type == 'Installation Partner'){
            ssc = new ApexPages.StandardSetController([SELECT Id, Name, Phone, Email__c, Type FROM Account WHERE Type='Installation Partner']);
        	ssc.setPageSize(10);
        }
    }
    
    
    public String options { get; set; }
    
    public PageReference executeSearch() {
        return null;
    }
    
    //public list<account> showList = new list<account>([select Name, Email__c,Phone from Account]);
    
    Public list<account> getshowList() {
        return (List<Account>)ssc.getRecords();
    }
    
    public PageReference create() {
        return null;
    }
    
    public pagereference search() { 
        string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20'; 
        acc= Database.query(searchquery);
        DisplaySearchResults =true;
        if(acc.size()==0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'account not found'));
            ShowCreateBlock = true; 
            ShowBlock=False;
        }
        return null;
    }
    
    public void clear() { 
        acc.clear(); 
    } 
    
    Public pagereference saveRecord() {
        acc=[select id,email__c from Account];
        for(Account a : acc) {
            if(NewAccountRecord.Email__c==a.Email__C) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Email is already exist'));
                return null;
            }
        }
        if(ApexPages.hasMessages()==false) {
            insert NewAccountRecord;
            pagereference  pageRef = new pagereference('/'+NewAccountRecord.Id);
            return pageRef ; 
        } 
        return null; 
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('10','10'));
        options.add(new SelectOption('20','20'));
        options.add(new SelectOption('30','30'));
        options.add(new SelectOption('40','40'));
        return options;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
HoysalaHoysala
Thanku so much Khan Anas, it helped me allot as well as i understood the code u wrote, excellent. U made me me understand the code...
Khan AnasKhan Anas (Salesforce Developers) 
Hi Suraj, 

It's my pleasure. I’m glad I was able to help!

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Regards,
Khan Anas