+ Start a Discussion
Rodolfo CalvoRodolfo Calvo 

How do I show all accounts in a selectList?

Hello team, 

I have this code: 
<apex:pageBlock>
              <apex:pageBlockSection title="Custom Picklist Using selectList and selectOption" collapsible="false">
                <apex:selectList value="{!selectedCountry1}" multiselect="false" size="1">
                    <apex:selectOption itemValue="INDIA" itemLabel="India"/>
                    <apex:selectOption itemValue="USA" itemLabel="USA"/>
                    <apex:selectOption itemValue="United Kingdom" itemLabel="UK"/>
                </apex:selectList>
                 
                <apex:outputText value="{!selectedCountry1}" label="You have selected:"/>
            </apex:pageBlockSection>
        </apex:pageBlock>

Controller
public String selectedCountry1{get;set;}

How can I show all my accounts in the selectList? I need an example of coding the method! 
Thanks in advance! 
Best Answer chosen by Rodolfo Calvo
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm
 
/*** Controller: ***/
    public class sampleCon {
        String[] countries = new String[]{};
            
        public PageReference test() {
            return null;
        }
            
        public List<SelectOption> getItems() 
		{
    
			List<Account> lstAcc = [select id,name from account limit 10];
			List<SelectOption> options = new List<SelectOption>();

			for(Account acc : lstAcc )
			{
				options.add( new SelectOption( acc.id,acc.Name ) );
			}
			
            return options;
        }
            
        public String[] getCountries() {
            return countries;
        }
            
        public void setCountries(String[] countries) {
            this.countries = countries;
        }
    }

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm
 
/*** Controller: ***/
    public class sampleCon {
        String[] countries = new String[]{};
            
        public PageReference test() {
            return null;
        }
            
        public List<SelectOption> getItems() 
		{
    
			List<Account> lstAcc = [select id,name from account limit 10];
			List<SelectOption> options = new List<SelectOption>();

			for(Account acc : lstAcc )
			{
				options.add( new SelectOption( acc.id,acc.Name ) );
			}
			
            return options;
        }
            
        public String[] getCountries() {
            return countries;
        }
            
        public void setCountries(String[] countries) {
            this.countries = countries;
        }
    }

 
This was selected as the best answer
jyothsna reddy 5jyothsna reddy 5
Hi Rodolfo Calvo,

The above scenario can be acheived through Dynamic Apex.
Please check the below link for sample code with explanation.
http://sfdcsrini.blogspot.com/2014/11/dynamic-apex-in-salesforce.html

I hope it will help you.

Regards,
Jyothsna D
 
Rupal KumarRupal Kumar
hi Rodolfo,
      See below link-
     https://www.minddigital.com/how-to-create-pagination-within-salesforce/

and Try this code-
<apex:page controller="ExtractSobject">
<apex:form >
   <apex:pageblock >
     <apex:pageblocksection >
       <apex:pageBlockSectionItem >
         <apex:outputlabel value="Select Object"/>
          <apex:selectList value="{!Selectedobject }" size="1">
            <apex:selectoptions value="{!Selectedobjnames}"></apex:selectoptions>
            <apex:actionSupport event="onchange" rerender="a"/>
         </apex:selectList>
       </apex:pageBlockSectionItem>
      
     <apex:pageBlockSectionItem >
      <apex:outputPanel id="a">
         <apex:outputLabel value="Object Fields" ></apex:outputLabel>
             <apex:selectList value="{!Sf}" size="1"> 
                   <apex:selectOptions value="{!SelectedobjFields}" />
             </apex:selectList>
      </apex:outputPanel>
     </apex:pageBlockSectionItem>
     
     
   </apex:pageBlockSection>   
  </apex:pageblock>
 </apex:form>
 
</apex:page>

controller*****

public with sharing class ExtractSobject
{
    public String Sf{get;set;}
    public String Selectedobject { get; set; }    
  
        public List<SelectOption> getSelectedobjnames()
        {
            List<Schema.SObjectType> obj = Schema.getGlobalDescribe().Values();
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('--Select Object--','--Select Object--'));
            for(Schema.SObjectType st : obj)
            {
                if(Selectedobject == null || Selectedobject=='' )
                {       
                    Selectedobject = st.getDescribe().getName();
                }
                options.add(new SelectOption(st.getDescribe().getName(),st.getDescribe().getName()));
            }
            return options;
        }
       
        public List<SelectOption> getSelectedobjFields()
        {
            SObjectType objTyp = Schema.getGlobalDescribe().get(Selectedobject);
            DescribeSObjectResult objDef = objTyp.getDescribe();
            Map<String, SObjectField> fields = objDef.fields.getMap();

            Set<String> fieldSet = fields.keySet();
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('--Select Object--','--Select Object--'));
            for(String s:fieldSet)
            {  
                SObjectField Sobjfields = fields.get(s);
                DescribeFieldResult selectedField = Sobjfields.getDescribe();                 
                options.add(new SelectOption(selectedField.getName(),selectedField.getName()));
            }
            return options;
        }
}

Thanks
RupalKumar
Mirketa Software Pvt Ltd
http://mirketa.com/index.html

 
Rodolfo CalvoRodolfo Calvo
Thanks a lot to all!!