Step
1: Create a VF page in which Object
should be list of picklist fields obtained from the schema
Step2
: When ever we select the particular
object from the picklist list of object based on the object what we have
selected we have to get the corresponding list of fields
Displayed
in the multiselect picklist field
Once
you select the list of fields from the multiselect picklist field then click on
getQuery
Apex
Class :
public class SchemaDynamicExample
{
public
Map<String,Schema.SobjectType> objMap {Set;get;}
public
Map<String,Schema.SobjectField> fldMap {set;get;}
public
List<String> objList {Set;get;}
public
List<SelectOption> objOptions{set;get;}
public String
objselected{set;get;}
public
List<String> fldList{set;get;}
public Set<String>
nsflds{set;get;}
public Set<String>
sflds{Set;get;}
public
List<SelectOption> nsoptions{set;get;}
public
List<SelectOption> soptions{set;get;}
public
List<String> selected{set;get;}
public
List<String> removed{set;get;}
public List<Account> accs{set;get;}
public String
query {Set;get;}
public
List<String> pbtflds{Set;get;}
public
SchemaDynamicExample(){
objList=new
List<String>();
objOptions=new List<SelectOption>();
fldList=new
List<String>();
nsflds=new Set<String>();
sflds=new Set<String>();
nsoptions=new
List<SelectOption>();
soptions=new
List<SelectOption>();
selected=new
List<String>();
removed=new
List<String>();
getObjects();
SelectOption op=new
SelectOption('none','-none-');
nsoptions.add(op);
soptions.add(op);
}
public void
getObjects(){
objMap=Schema.getGlobalDescribe();
Set<String>
keys=objMap.keySet();
objList.addAll(keys);
objList.sort();
SelectOption op=new
SelectOption('none','-None-');
objoptions.add(op);
for(String
s:objList){
selectOption op1=new
SelectOption(s,s);
objOptions.add(op1);
}
}
public void
getResults(){
query='select id';
for(string
s:sflds){
if(s!='id'){
query=query+','+s;
}
}
query=query+' from Account';
accs=Database.query(query);
pbtflds=new
List<String>();
pbtflds.addAll(sflds);
}
public void
getFields(){
Schema.DescribeSobjectResult
result=objMap.get(objSelected).getDescribe();
fldmap=result.fields.getMap();
Set<String>
keys=fldmap.keySet();
fldList.addAll(keys);
fldList.sort();
nsflds.addAll(fldList);
multiselect();
}
public void multiselect(){
nsoptions.clear();
soptions.clear();
List<String>
slist=new List<String>();
slist.addAll(sflds);
slist.sort();
List<String>
nslist=new List<String>();
nslist.addAll(nsflds);
nslist.sort();
for(String
s1:nslist){
SelectOption
op1=new SelectOption(s1,s1);
nsoptions.add(op1);
}
for(String
s2:slist){
SelectOption
op2=new SelectOption(s2,s2);
soptions.add(op2);
}
}
public void addEle(){
nsflds.removeAll(Selected);
sflds.addAll(selected);
multiselect();
}
public void
removeEle(){
sflds.removeAll(removed);
nsflds.addAll(removed);
multiSelect();
}
}
Visualforce page:
<apex:page controller="SchemaDynamicExample" >
<apex:form>
<apex:pageBlock title="Schema Dynamic List">
<apex:pageBlockSection>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Objects" />
<apex:selectList value="{!objSelected}" size="1">
<apex:selectOptions value="{!objOptions}" />
<apex:actionSupport event="onchange" action="{!getFields}"
reRender="pg"/>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem>
<apex:panelGrid id="pg" columns="3">
<apex:selectList value="{!selected}" multiselect="true" style="width:100px;height:200px;" >
<apex:selectOptions value="{!nsoptions}" />
</apex:selectList>
<apex:panelGrid columns="1">
<br/>
<apex:commandButton value="Add" action="{!addEle}"
reRender="pg" style="width:50px;" />
<br/>
<apex:commandButton value="Del" action="{!removeEle}"
reRender="pg" style="width:50px;"/>
</apex:panelGrid>
<apex:selectList value="{!removed}" multiselect="true" style="width:100px;height:200px;" >
<apex:selectOptions value="{!soptions}" />
</apex:selectList>
</apex:panelGrid>
</apex:pageBlockSectionItem>
<apex:commandButton value="GetQuery" action="{!getResults}"
reRender="pb2" />
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock id="pb2">
<apex:pageBlockTable value="{!accs}" var="a">
<apex:repeat value="{!pbtflds}" var="b">
<apex:column value="{!a[b]}" />
</apex:repeat>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
No comments:
Post a Comment