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
tim111tim111 

Lead values on VF page...????

I have created a VF page  where i have put lead fields in custom picklist(picklist name is FIELD NAME).Now i want "when i choose any field name i want to get the values of lead. For eg when i choose "status" fieldname  a picklist should show as it is there on Lead object , when i choose "industry" fieldname a picklist show showing all the industries.Please help...i am new  to apex.please explain with code.help will br appreciated....

Best Answer chosen by Admin (Salesforce Developers) 
apex_keenapex_keen

You can use this code : get back to me in case of any issues. 

 

public class describeLeadController {
             
       Public string currentSelectedOption{get;set;}
       Public string FinalValue {get;set;}
        Public list<selectOption> FinalEachValue{get;set;}
       public string finalOutVar{get;set;} 
      
      public list<SelectOption> EachOption {
      
        get { 
                SObjectType objToken = Schema.getGlobalDescribe().get('lead');
                DescribeSObjectResult objDef = objToken.getDescribe();
                Map<String, SObjectField> fields = objDef.fields.getMap();
                 Set<String> fieldSet = fields.keySet();
                list<string> FieldList = new list<string>(fieldset);
                 List<selectOption> options = new List<selectOption>();
 
                  for(string str : FieldList) 
                  Options.add(new selectOption(str,str));
                 
                return Options;
            }  
         set ;
       }
     
     public string ShowFieldValues() 
     { 
                 List<selectOption> Finaloptions = new List<selectOption>();
                SObjectType objToken = Schema.getGlobalDescribe().get('lead');
                DescribeSObjectResult objDef = objToken.getDescribe();
                Map<String, SObjectField> fieldsMap = objDef.fields.getMap();   
             List<Schema.PicklistEntry> pick_list_values = fieldsMap.get(currentSelectedOption).getDescribe().getPickListValues();   
                   for (Schema.PicklistEntry a : pick_list_values) 
                   FinalOptions.add(new selectOption(a.getValue(),a.getValue()));
    
              FinalEachValue = FinalOptions;
               return null ;
    }
}
=================================================================
<apex:page controller="describeLeadController" >
 <apex:form >
 <apex:pageblock title="All about leads" >
  <apex:selectList value="{!currentSelectedOption}" size="1">
   <apex:selectOptions value="{!EachOption}" />
   <apex:actionSupport event="onchange" action="{!ShowFieldValues}">
       <apex:param id="mysel" value="{!currentSelectedOption}" assignTo="{!currentSelectedOption}" />
   </apex:actionSupport>
  </apex:selectList>
  </apex:pageblock>
   <apex:outPutPanel > 
    <apex:selectList value="{!FinalValue}" >
      <apex:selectOptions value="{!FinalEachValue}" />
    </apex:selectList>
 </apex:outPutPanel>
 </apex:form>
 </apex:page>

 

All Answers

apex_keenapex_keen

You can use this code : get back to me in case of any issues. 

 

public class describeLeadController {
             
       Public string currentSelectedOption{get;set;}
       Public string FinalValue {get;set;}
        Public list<selectOption> FinalEachValue{get;set;}
       public string finalOutVar{get;set;} 
      
      public list<SelectOption> EachOption {
      
        get { 
                SObjectType objToken = Schema.getGlobalDescribe().get('lead');
                DescribeSObjectResult objDef = objToken.getDescribe();
                Map<String, SObjectField> fields = objDef.fields.getMap();
                 Set<String> fieldSet = fields.keySet();
                list<string> FieldList = new list<string>(fieldset);
                 List<selectOption> options = new List<selectOption>();
 
                  for(string str : FieldList) 
                  Options.add(new selectOption(str,str));
                 
                return Options;
            }  
         set ;
       }
     
     public string ShowFieldValues() 
     { 
                 List<selectOption> Finaloptions = new List<selectOption>();
                SObjectType objToken = Schema.getGlobalDescribe().get('lead');
                DescribeSObjectResult objDef = objToken.getDescribe();
                Map<String, SObjectField> fieldsMap = objDef.fields.getMap();   
             List<Schema.PicklistEntry> pick_list_values = fieldsMap.get(currentSelectedOption).getDescribe().getPickListValues();   
                   for (Schema.PicklistEntry a : pick_list_values) 
                   FinalOptions.add(new selectOption(a.getValue(),a.getValue()));
    
              FinalEachValue = FinalOptions;
               return null ;
    }
}
=================================================================
<apex:page controller="describeLeadController" >
 <apex:form >
 <apex:pageblock title="All about leads" >
  <apex:selectList value="{!currentSelectedOption}" size="1">
   <apex:selectOptions value="{!EachOption}" />
   <apex:actionSupport event="onchange" action="{!ShowFieldValues}">
       <apex:param id="mysel" value="{!currentSelectedOption}" assignTo="{!currentSelectedOption}" />
   </apex:actionSupport>
  </apex:selectList>
  </apex:pageblock>
   <apex:outPutPanel > 
    <apex:selectList value="{!FinalValue}" >
      <apex:selectOptions value="{!FinalEachValue}" />
    </apex:selectList>
 </apex:outPutPanel>
 </apex:form>
 </apex:page>

 

This was selected as the best answer
tim111tim111

Thank you so much.....it solved my problem....