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
sushmaiyer2@gmail.comsushmaiyer2@gmail.com 

How to get Selected Object's Fields in a Multi select Picklist

Hi All,

 

I have a requirement to get the Selected Object's Fields in a Multi select Picklist.Currently i am able to select the Object and get its fields in a list.

 

Below is my VF Code :-

 

<apex:page Controller="Describer">
<apex:form id="Describe">
<apex:pageBlock id="block2" >
<apex:pageblockbuttons location="top" >
<apex:commandButton value="Get Describe Object Fields" action="{!showFields}"/>
</apex:pageblockbuttons>
<apex:pageblocksection >
<apex:selectList value="{!selectedObject}" size="1">
<apex:selectOptions value="{!objectNames}"/>
</apex:selectList>
</apex:pageblocksection>
<apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
<apex:panelBar items="{!fields}" var="fls">
<apex:panelBarItem label="{!fls.key}">{!fls.val}</apex:panelBarItem>
</apex:panelBar>
</apex:pageblocksection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Below is my Controller Code :

 

public class Describer {

private Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public List <Pair> fields {get; set;}
public List <SelectOption> objectNames {public get; private set;}
public String selectedObject {get; set;}

// Intialize objectNames and fields

public Describer() {
objectNames = initObjNames();
fields = new List<Pair>();
}
// Populate SelectOption list -

// find all sObjects available in the organization

private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();

List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
for(String name : entities)
objNames.add(new SelectOption(name,name));
return objNames;
}

// Find the fields for the selected object

public void showFields() {
fields.clear();
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();

for(Schema.SObjectField sfield : fieldMap.Values()){
schema.describefieldresult dfield = sfield.getDescribe();
Pair field = new Pair();
field.key = dfield.getname();
fields.add(field);
}
}


public class Pair {
public String key {get; set;}
public String val {get; set;}
}
}

swatKatswatKat

Change these portions :

 

VF Page

 

 <apex:pageblocksection >
            <apex:selectList value="{!selectedObject}" size="1">
                <apex:selectOptions value="{!objectNames}"/>
                <apex:actionSupport event="onchange" rerender="Describe" action="{!showFields}"/>
            </apex:selectList>
        </apex:pageblocksection>
        <apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
            <apex:selectList value="{!selectedFields}" multiselect="true">
                <apex:selectOptions value="{!fieldNames}"/>
            </apex:selectList>
        </apex:pageblocksection>

 

Apex Class :

 

List<SelectOption> fieldNames = new List<SelectOption>();
for(Schema.SObjectField sfield : fieldMap.Values()){
    schema.describefieldresult dfield = sfield.getDescribe();
    fieldNames.add(new SelectOption(dfield.getName(),dfield.getLabel()));
}

 

 

sfdc18sfdc18

Hi SwatKat,

 

Can you please explain how to define selectedFields in controller

 

 

<apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
            <apex:selectList value="{!selectedFields}" multiselect="true">
                <apex:selectOptions value="{!fieldNames}"/>
            </apex:selectList>
        </apex:pageblocksection>

 

Regards,

Mayur

Avidev9Avidev9
selectedFields should be a String variable at class level, which whill store whatever is selected from the UI.

something like

public String selectedFields{get;set;}
sushmaiyer2@gmail.comsushmaiyer2@gmail.com

Hi Swakat,

 

I am still getting error in my Controller Code  :-

Error is :- Error: dynamicreportscontroller Compile Error: expecting a semi-colon, found '(' at line 41 column 40

 

public class dynamicreportscontroller {

private Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public List <Pair> fields {get; set;}
public List <SelectOption> objectNames {public get; private set;}
public String selectedObject {get; set;}
public String selectedFields {get; set;}
//public List<SelectOption> fieldNames = new List<SelectOption>();
public List<SelectOption> fieldNames {public get; private set;}

// Intialize objectNames and fields
public dynamicreportscontroller() {
objectNames = initObjNames();
fields = new List<Pair>();
fieldNames = getFieldNames();
}

// Populate SelectOption list -
// find all sObjects available in the organization

private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();
List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
for(String name : entities)
objNames.add(new SelectOption(name,name));
return objNames;
}

// Find the fields for the selected object
public void showFields() {
fields.clear();
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values()){
schema.describefieldresult dfield = sfield.getDescribe();
Pair field = new Pair();
field.key = dfield.getname();
fields.add(field);
}

private List<SelectOption> getFieldNames() {                  Shows Error in this line
List<SelectOption> fieldNames = new List<SelectOption>();
for(Schema.SObjectField sfield : fieldMap.Values())
{
    schema.describefieldresult dfield = sfield.getDescribe();
    fieldNames.add(new SelectOption(dfield.getName(),dfield.getLabel()));
}
}

public class Pair
{
public String key {get; set;}
public String val {get; set;}
}

}
}

 

VF Code is :-

<apex:page Controller="dynamicreportscontroller">
<apex:form id="Describe">
<apex:pageBlock id="block2" >
<apex:pageblockbuttons location="top" >
<apex:commandButton value="Get Describe Object Fields" action="{!showFields}"/>
</apex:pageblockbuttons>
<apex:pageblocksection >
<apex:selectList value="{!selectedObject}" size="1">
<apex:selectOptions value="{!objectNames}"/>
</apex:selectList>
</apex:pageblocksection>

<apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
            <apex:selectList value="{!selectedFields}" multiselect="true">
                <apex:selectOptions value="{!fieldNames}"/>
            </apex:selectList>
</apex:pageblocksection>
</apex:pageBlock>
</apex:form>
</apex:page>

swatKatswatKat

You have misplaced the ending curly brace for the method "showFields". Just correct that and it should be fine 

Gopesh Vardhan Singh 8Gopesh Vardhan Singh 8
What's the final solution to this query ???