how to retrieve and display the list of objects as a dropdown list in visualforce and then use the selected value in my controller to return the field of that object.
public class ControllerGetAllObject { public List<SelectOption> listOfObjects{get;set;} public List<SelectOption> listOfFields{get;set;} public String selectedObject{get;set;} public String selectedField{get;set;} public Map<String, Schema.SobjectType> gd = schema.getGlobalDescribe(); public Map<String, schema.SObjectField> MapOfFields; public ControllerGetAllObject(){ listOfObjects = new List<SelectOption>();
2 things you could so. 1. get the sObject info via Apex: Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); and then further in the apex using the selected value as a paramter, u can retrive the list of the respective sObject fields.
2. Custom metadata type is another solution where you can get the schema info.
I cannot explain in detail at the moment, but if you have further questions, feel free to ask. I will try to reply tomorrow. :-)
public class ControllerGetAllObject { public List<SelectOption> listOfObjects{get;set;} public List<SelectOption> listOfFields{get;set;} public String selectedObject{get;set;} public String selectedField{get;set;} public Map<String, Schema.SobjectType> gd = schema.getGlobalDescribe(); public Map<String, schema.SObjectField> MapOfFields; public ControllerGetAllObject(){ listOfObjects = new List<SelectOption>();
LWC :how can i display the list of Objects in a drop down list and when we select an objects it should display it fields. i want in lwc could you please help me
Visualforce page:
<apex:page controller="ControllerGetAllObject" >
<apex:form >
<apex:actionFunction name="CallFieldMethod" action="{!fetchFields}" reRender="fields" />
<apex:actionFunction name="CallField" action="{!selectedField}" />
<apex:pageMessages id="msg"></apex:pageMessages>
<apex:outputPanel id="objectlist" >
Select Object:
<apex:selectList value="{!selectedObject}" size="1" onchange="CallFieldMethod()" >
<apex:selectOptions value="{!listOfObjects}"></apex:selectOptions>
</apex:selectList>
</apex:outputPanel><br/>
<apex:outputPanel id="fields" >
Select Field :
<apex:selectList value="{!selectedField}" size="1" id="FieldList" rendered="{!selectedObject != null}" onchange="CallField()" >
<apex:selectOptions value="{!listOfFields}"></apex:selectOptions>
</apex:selectList>
</apex:outputPanel>
</apex:form>
</apex:page>
Controller:
public class ControllerGetAllObject {
public List<SelectOption> listOfObjects{get;set;}
public List<SelectOption> listOfFields{get;set;}
public String selectedObject{get;set;}
public String selectedField{get;set;}
public Map<String, Schema.SobjectType> gd = schema.getGlobalDescribe();
public Map<String, schema.SObjectField> MapOfFields;
public ControllerGetAllObject(){
listOfObjects = new List<SelectOption>();
for(Schema.SObjectType obj : gd.values()){
Schema.DescribeSObjectResult dr = obj.getDescribe();
listOfObjects.add(new SelectOption(String.valueOf(dr.getName()), String.valueOf(dr.getLabel())));
}
listOfObjects.sort();
}
public void fetchFields(){
listOfFields = new List<SelectOption>();
MapOfFields = gd.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField field : MapOfFields.values()){
Schema.DescribeFieldResult fr = field.getDescribe();
listOfFields.add( new SelectOption(String.valueof(fr.getName()), String.valueof(fr.getLabel())));
}
}
public void selectedField(){
system.debug(selectedField);
}
}
I hope this will help you.
Please mark as best answer if this solves your problem.
Thank you!
All Answers
1. get the sObject info via Apex:
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
and then further in the apex using the selected value as a paramter, u can retrive the list of the respective sObject fields.
2. Custom metadata type is another solution where you can get the schema info.
I cannot explain in detail at the moment, but if you have further questions, feel free to ask. I will try to reply tomorrow. :-)
Cheers,
Dev
Hello,
/** add this in vf page **/
/** add this in controller **/
I hope, this will help you,
Thank you
Visualforce page:
<apex:page controller="ControllerGetAllObject" >
<apex:form >
<apex:actionFunction name="CallFieldMethod" action="{!fetchFields}" reRender="fields" />
<apex:actionFunction name="CallField" action="{!selectedField}" />
<apex:pageMessages id="msg"></apex:pageMessages>
<apex:outputPanel id="objectlist" >
Select Object:
<apex:selectList value="{!selectedObject}" size="1" onchange="CallFieldMethod()" >
<apex:selectOptions value="{!listOfObjects}"></apex:selectOptions>
</apex:selectList>
</apex:outputPanel><br/>
<apex:outputPanel id="fields" >
Select Field :
<apex:selectList value="{!selectedField}" size="1" id="FieldList" rendered="{!selectedObject != null}" onchange="CallField()" >
<apex:selectOptions value="{!listOfFields}"></apex:selectOptions>
</apex:selectList>
</apex:outputPanel>
</apex:form>
</apex:page>
Controller:
public class ControllerGetAllObject {
public List<SelectOption> listOfObjects{get;set;}
public List<SelectOption> listOfFields{get;set;}
public String selectedObject{get;set;}
public String selectedField{get;set;}
public Map<String, Schema.SobjectType> gd = schema.getGlobalDescribe();
public Map<String, schema.SObjectField> MapOfFields;
public ControllerGetAllObject(){
listOfObjects = new List<SelectOption>();
for(Schema.SObjectType obj : gd.values()){
Schema.DescribeSObjectResult dr = obj.getDescribe();
listOfObjects.add(new SelectOption(String.valueOf(dr.getName()), String.valueOf(dr.getLabel())));
}
listOfObjects.sort();
}
public void fetchFields(){
listOfFields = new List<SelectOption>();
MapOfFields = gd.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField field : MapOfFields.values()){
Schema.DescribeFieldResult fr = field.getDescribe();
listOfFields.add( new SelectOption(String.valueof(fr.getName()), String.valueof(fr.getLabel())));
}
}
public void selectedField(){
system.debug(selectedField);
}
}
I hope this will help you.
Please mark as best answer if this solves your problem.
Thank you!