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
munna123munna123 

how to get only the list of selected objects as a dropdown

here in the below code am getting the list of all objects in an organisation. but i need only to select only few objects like account, contact, solutions etc.. how to get this.

<apex:page controller="dynamic1" >
    <apex:form id="op">
        <apex:pageBlock >
            <apex:pageBlockSection id="od1" columns="1">
                <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Object List"></apex:outputLabel>
                    <apex:selectList size="1" value="{!selectedobj}">
                        <apex:selectOptions value="{!objects}" ></apex:selectOptions>
                        <apex:actionSupport action="{!details}" event="onchange" rerender="op"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                <br/>
                <br/>
                <br/>         
                <apex:pageBlockSection >
               <apex:pageBlockSectionItem >
           <apex:outputLabel value="fields List:"></apex:outputLabel>

                    <apex:panelGrid columns="3" id="od" >
                        <apex:panelGroup style="width:40%" >
                            <apex:selectList size="6" value="{!sfields}" multiselect="true" style="width:200px;hieght:150px;">
                                <apex:selectOptions value="{!fnoption}"></apex:selectOptions>
                            </apex:selectList>
                        </apex:panelGroup>
                        <apex:panelGroup style="width:20%;hieght:120px;">
                            <table hieght="120px">
                                <tr><td><apex:commandButton value="Add" action="{!addfields}" style="width:80px;" reRender="od"  /> </td></tr>
                                <tr><td><apex:commandButton value="Remove" action="{!removefields}" style="width:80px;" reRender="od1"  /> </td></tr>
                            </table>
                        </apex:panelGroup>
                        <apex:panelGroup >
                            <apex:selectList size="6"  value="{!rfields}" multiselect="true" style="width:200px;hieght:150px;">
                                <apex:selectOptions value="{!fsoption}"></apex:selectOptions>
                            </apex:selectList>
                        </apex:panelGroup>
                    </apex:panelGrid>
                </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                <apex:pageBlockSectionItem >
                    <apex:pageBlockTable value="{!mydata}" var="a" >
                        <apex:repeat value="{!myfields}" var="b">
                            <apex:column value="{!a[b]}"/>
                        </apex:repeat>
                    </apex:pageBlockTable>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <br/>
            <br/>
            <br/>
            <apex:commandButton value="click" action="{!querymydata}" reRender="op"/>{!query}
        </apex:pageBlock>    
    </apex:form>
</apex:page>

controller:
-----------------
 public class dynamic1 {
    
    public list<selectoption>objects{set;get;}
    public list<selectoption>fsoption{set;get;}
    public list<selectoption>fnoption{set;get;}
    public list<string> sfields{set;get;}
    public list<string> rfields{set;get;}
    public list<string>objlst{set;get;}
    public list<string>fields{set;get;}
    public set<string>fselected{set;get;}
    public set<string>fnotselected{set;get;}
    public map<string,schema.SObjectField> mfields;
    public map<string,schema.SObjectType> mobjects{set;get;}
    public string selectedobj{set;get;}
    public string objdiscription{set;get;}
    public string query{set;get;}
    public list<string>myfields{set;get;} 
    public list<sobject>mydata{set;get;}
    public dynamic1(){
        myfields=new list<string>();
        mydata=new list<sobject>();
        objects=new list<selectoption>();
        objlst=new list<string>();
        sfields=new list<string>();
        rfields=new list<string>();
        fields=new list<string>();
        fselected=new set<string>();
        fnotselected=new set<string>();
        fsoption=new list<selectoption>();
        fnoption=new list<selectoption>();
        mobjects=schema.getGlobalDescribe();
        objlst.addall(mobjects.keyset());
            objlst.sort();
        selectoption p=new selectoption('none','-none-');
        objects.add(p);
        for(string s:objlst){
            selectoption op=new selectoption(s,s);
            objects.add(op);
        }
        
    }
    
    public void details(){
        schema.DescribeSObjectResult mobj=mobjects.get(selectedobj).getDescribe();
        mfields=mobjects.get(selectedobj).getDescribe().fields.getMap();
        fields.addAll(mfields.keySet());
        fnotselected.addAll(fields);
        show();
    }
    public void show(){
 fnoption.clear();
        fsoption.clear();
        for(string s:fnotselected){
            selectoption op=new selectoption(s,s);
            fnoption.add(op);
        }
        for(string s1:fselected){
            selectoption op1=new selectoption(s1,s1);
            fsoption.add(op1);
            
        }
        
    }
    public void addfields(){
        fnotselected.removeAll(sfields);
        fselected.addAll(sfields);
        show();
    }
    public void removefields(){
        fnotselected.addAll(rfields);
        fselected.removeAll(rfields);
        show();
    }
    public void querymydata(){
         query ='select id';
        myfields.addAll(fselected);
        for(string s:fselected){
            query=query+','+s;
        }
        query=query + ' from ' + selectedobj;
        mydata=database.query(query);
    }
}