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
niharnihar 

I want to create a picklist field in visual force page.and values are Lead and contact objects?


When i select the drop down the Lead or Contact object how can we see all Lead and contact records in visual force page. will you Plz suggest how to write code & How to create VF Page ?

Thank You
Best Answer chosen by nihar
Khan AnasKhan Anas (Salesforce Developers) 
Hi Nihar,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="ObjInPicklistC">
    <apex:form >
        <apex:pageBlock > 
            <apex:pagemessages />
            <apex:pageBlockSection >
                <apex:outputLabel value="Object Names :"/>
                <apex:selectList value="{!selectedObject}" size="1">
                    <apex:selectOption itemLabel="--None--" itemValue="--None--"/>
                    <apex:selectOptions value="{!objectNames}"/>
                    <apex:actionSupport event="onchange" action="{!fetchFields}" rerender="flds"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageblock id="flds" title="Last 50 Records">
            <apex:pagemessages />
            <apex:pageblockTable value="{!objFields}" var="fd">
                <apex:repeat value="{!result}" var="res">
                    <apex:column value="{!fd[res]}" />
                </apex:repeat>
            </apex:pageblockTable>
        </apex:pageblock>    
    </apex:form>
</apex:page>

Controller:
public class ObjInPicklistC {
    
    public String selectedObject {get;set;}
    public List<sObject> objFields {get;set;} 
    public List<String> result {get;set;}
    
    public ObjInPicklistC(){ 
        selectedObject = 'none';
    }
    
    public List<SelectOption> getobjectNames(){  
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Lead','Lead'));
        options.add(new SelectOption('Contact','Contact'));
        return options;
    }
    
    public void fetchFields(){ 
        result = new List<String>();
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType objType = globalDescription.get(selectedObject);   
        Schema.DescribeSObjectResult r1 = objType.getDescribe(); 
        
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        
        for(Schema.SObjectField field : mapFieldList.values()){  
            Schema.DescribeFieldResult fieldResult = field.getDescribe();  
            
            if(fieldResult.isAccessible()){  
                result.add(fieldResult.getName());
            }  
        }
        Integer i = 0;
        String fieldsToFetch = '';
        for(String temp:result){       
            Integer len = result.size();
            if(i==len-1){
                fieldsToFetch = fieldsToFetch + temp;
            }
            else{
                fieldsToFetch = fieldsToFetch + temp + ',';
            }
            i++;
        }
        try{
            String sql = ' SELECT ' + fieldsToFetch + ' FROM ' + selectedObject + ' ORDER BY CreatedDate DESC LIMIT 50';
            objFields = Database.Query(sql);
        }
        catch(Exception e){
            ApexPages.addMessages(e);
        }
    }   
}

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. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Nubes Elite Technologies Pvt. LtdNubes Elite Technologies Pvt. Ltd
Hi Nihar,

You can try the below code -

Change the object as per your need to Lead and Contact.
 
<apex:page controller="accountfiltercon ">
    <apex:form >
    <apex:selectList size="1" value="{!selectedname}"> 
        <apex:selectOptions value="{!selectedaccnamefields}"/>  
    </apex:selectList>
    </apex:form>
</apex:page>

public class accountfiltercon {
    Public string selectedname{get;set;}
        Public List<Selectoption> getselectedaccnamefields(){
            List<Selectoption> lstnamesel = new List<selectoption>();
            lstnamesel.add(new selectOption('', '- None -'));
            for(Account acc :[SELECT id,name,phone,type,industry FROM Account]){
            lstnamesel.add(new selectoption(acc.id,acc.name));
            }
            return lstnamesel; 
        }
}

Thank You,
www.nubeselite.com
Development | Training | Consulting

Please mark this as solution if your problem is solved.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Nihar,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="ObjInPicklistC">
    <apex:form >
        <apex:pageBlock > 
            <apex:pagemessages />
            <apex:pageBlockSection >
                <apex:outputLabel value="Object Names :"/>
                <apex:selectList value="{!selectedObject}" size="1">
                    <apex:selectOption itemLabel="--None--" itemValue="--None--"/>
                    <apex:selectOptions value="{!objectNames}"/>
                    <apex:actionSupport event="onchange" action="{!fetchFields}" rerender="flds"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageblock id="flds" title="Last 50 Records">
            <apex:pagemessages />
            <apex:pageblockTable value="{!objFields}" var="fd">
                <apex:repeat value="{!result}" var="res">
                    <apex:column value="{!fd[res]}" />
                </apex:repeat>
            </apex:pageblockTable>
        </apex:pageblock>    
    </apex:form>
</apex:page>

Controller:
public class ObjInPicklistC {
    
    public String selectedObject {get;set;}
    public List<sObject> objFields {get;set;} 
    public List<String> result {get;set;}
    
    public ObjInPicklistC(){ 
        selectedObject = 'none';
    }
    
    public List<SelectOption> getobjectNames(){  
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Lead','Lead'));
        options.add(new SelectOption('Contact','Contact'));
        return options;
    }
    
    public void fetchFields(){ 
        result = new List<String>();
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType objType = globalDescription.get(selectedObject);   
        Schema.DescribeSObjectResult r1 = objType.getDescribe(); 
        
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        
        for(Schema.SObjectField field : mapFieldList.values()){  
            Schema.DescribeFieldResult fieldResult = field.getDescribe();  
            
            if(fieldResult.isAccessible()){  
                result.add(fieldResult.getName());
            }  
        }
        Integer i = 0;
        String fieldsToFetch = '';
        for(String temp:result){       
            Integer len = result.size();
            if(i==len-1){
                fieldsToFetch = fieldsToFetch + temp;
            }
            else{
                fieldsToFetch = fieldsToFetch + temp + ',';
            }
            i++;
        }
        try{
            String sql = ' SELECT ' + fieldsToFetch + ' FROM ' + selectedObject + ' ORDER BY CreatedDate DESC LIMIT 50';
            objFields = Database.Query(sql);
        }
        catch(Exception e){
            ApexPages.addMessages(e);
        }
    }   
}

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. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer