Visualforce Page
<apex:page controller="DynamicSoqlExApplikonctr">
<apex:form >
<apex:pageBlock >
<Apex:pageBlockSection columns="1">
<Apex:pageBlockSectionItem >
Object Name
<Apex:selectList Size="1" multiselect="false" value="{!selectedObject}">
<apex:selectOptions value="{!ObjectName}" ></apex:selectOptions>
<apex:actionSupport event="onchange" reRender="pb1"/>
</Apex:selectList>
</Apex:pageBlockSectionItem>
<Apex:pageBlockSectionItem >
<apex:outputPanel id="pb1">
Field Name
<Apex:selectList Size="5" multiselect="True" value="{!selectedfield}">
<apex:selectOptions value="{!ObjectField}"></apex:selectOptions>
</Apex:selectList>
</apex:outputPanel>
</Apex:pageBlockSectionItem>
</Apex:pageBlockSection>
<CENTER><apex:commandButton value="Make table" action="{!MakeDynamicSOQLQuery}"/></CENTER>
<Apex:pageBlockSection >
<Apex:pageBlockSectionItem >
Dynamic Query:
<apex:inputTextarea rows="10" cols="60" value="{!temp1}"/>
</Apex:pageBlockSectionItem>
</Apex:pageBlockSection>
<apex:pageblockTable value="{!temp3}" var="t" rendered="{!IF(temp3.Size>0,true,false)}" >
<Apex:repeat value="{!selectedfield}" var="s" >
<Apex:column value="{!t[s]}" />
</apex:repeat>
</apex:pageblockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex class
/*
@Author - Nitesh Kumawat
Date - 25 july 2016
Requirement -
1. Select Object Name through picklist
2. automatically related field shown in another picklist
then make dynamic soql query
3. and then show table
*/
public class DynamicSoqlExApplikonctr
{
public Map<string,Schema.SObjectType> SchemaMap=Schema.getGlobalDescribe();
public string temp1{get;set;}
public list<SObject> temp3{get;set;}
public String selectedObject{get;set;}
public List<string> selectedfield{get;set;}
public DynamicSoqlExApplikonctr()
{
System.debug('@@SchemaMap-->'+SchemaMap);
selectedObject='none';
selectedfield=new list<String>();
system.debug('@@temp3'+temp3);
temp3=new list<SObject>();
}
//Object Name
public List<SelectOption> getObjectName()
{
List<SelectOption> objName=new List<SelectOption>();
List<String> schemaMapKeyList=new List<String>(SchemaMap.keySet());
system.debug('@@selectedObject-->'+selectedObject);
objName.add(new SelectOption('none','none'));
for(String name:schemaMapKeyList)
{
objName.add(new SelectOption(name,name));
}
return objName;
}
//Field Name
public List<SelectOption> getObjectField()
{
List<SelectOption> objField=new List<SelectOption>();
if(selectedObject!='none')
{
Schema.SObjectType selectObj=SchemaMap.get(selectedObject);
Map<string,Schema.SObjectField> SelectedObjectField=selectObj.getDescribe().fields.getMap();
system.debug('@@SelectedObjectField-->'+SelectedObjectField);
for(string fieldName:SelectedObjectField.keySet())
{
objField.add(new SelectOption(fieldName,fieldName));
}
}
return objField;
}
//Page Table
public void MakeDynamicSOQLQuery() {
system.debug('@@SelectedObject-->'+selectedObject);
system.debug('@@Selectedfield-->'+selectedfield);
temp1='Select ';
for(String s:selectedfield )
{
if(s!=selectedfield.get(selectedfield.Size()-1))
{
temp1+=s+','+' ';
}
else
{
temp1+=s+' ';
}
}
temp1+=' from '+selectedObject;
temp3=database.query(temp1);
}
}
Hi, how do I show records based on 2 picklist. Example: plicklist1 - Status , Picklist2 - skills. Based on the picks show the contacts.
ReplyDeleteBy the way great website with lots of resources. Keep up the great work!
ReplyDelete