You need to sign in to do that
Don't have an account?
munna123
how to get the list of only 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);
}
}
<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);
}
}
Try by filtering the records by using sObjectDescribe.isSearchable() && sObjectDescribe.isUpdateable() which will filter the objects only if they are updatable and searchable
Check the below code for your reference
Regards,
Bhanu Mahesh
Public static Map<String,String> getAllSObject()
{
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Map<String,String> mapSobject = new Map<String,String>();
for(String strObjectName : gd.keySet())
{
Schema.DescribeSObjectResult sObj = gd.get(strObjectName).getDescribe();
if(sObj.isAccessible() && sObj.isQueryable() && !sObj.isCustomSetting())
mapSobject.put(sObj.getName(),sObj.getLabel());
}
return mapSobject;
}
objlst .add('Account);
objlst .add('Contact);
objlst .add('Lead);
objlst .add('Opportunity);
Then the remaining works. If you eant to add the add any object make sure you are adding the API Name as value to SelectOption.
So it is better to add the Select Options directly
for(string s:objlst){
selectoption op=new selectoption(s,s);
objects.add(op);
}
Try this
objects.add(new selectoption('Account','Account'));
objects.add(new selectoption('Contact','Contact'));
objects.add(new selectoption('Lead','Lead'));
objects.add(new selectoption('Opportunity','Opportunity'));
If you want any custom object then
objects.add(new selectoption('API NAME','LABEL'));
Best practice is not to hardcode. use Custom setting or something to get the object names.
Regards,
Bhanu Mahesh
Try the below code. I have made changes in dynamic1() and deatils() methods
Regards,
Bhanu Mahesh
Add mydata.clear() in details() method.
Regards,
Bhanu Mahesh
Try this
VF Page:
Controller:
Close the discussion as solved if it meets your requirement
Regards,
Bhanu Mahesh
Try below code which will give you Notes and Attachments as well
VF Page
Controller
Close the thread if it meets your requirement
Regards,
Bhanu Mahesh