You need to sign in to do that
Don't have an account?
Manoj Goswami 5
Create DropDown list in Visualforce by taking values from Controller (getting unexpected error)
I want a list of all Objects in my org and displaying it in a drop down in Visualforce page. The code has compiled but giving unexpected error while getting Preview of VF page.
Controller :
Controller :
// Controller public without sharing class getObjectNamesController { public Map<String, Schema.SObjectType> object_Map = Schema.getGlobalDescribe(); public Set<String> objNamesSet = object_Map.keyset(); public List<SelectOption> objNamesList{ get{ Integer i=0; for(String myObjName : objNamesSet) { objNamesList.add(new SelectOption(String.valueOf(i),myObjName)) ; i++; } System.debug(objNamesList); return objNamesList ; } set; } }VF Page:
<apex:page controller="getObjectNamesController"> <apex:form > <apex:selectList size="1"> <apex:selectOptions value="{!objNamesList}"></apex:selectOptions> </apex:selectList> </apex:form> </apex:page>
Update below code in your controller class
// Controller
public without sharing class getObjectNamesController {
public Map<String, Schema.SObjectType> object_Map = Schema.getGlobalDescribe();
public Set<String> objNamesSet = object_Map.keyset();
public List<SelectOption> objNamesList{
get{
List<SelectOption> options = new List<SelectOption>();
Integer i=0;
for(String myObjName : objNamesSet)
{
options.add(new SelectOption(String.valueOf(i),myObjName)) ;
i++;
}
System.debug(objNamesList);
return options;
}
set;
}
}
Let me know for any issues.
Thanks
Arpit
Please mark this answer as SOLVED and BEST ANSWER if it helps you.
All Answers
And why are you using integer i here? you can simply do it like - objNamesList.add(new SelectOption(myObjName,myObjName)) ;
Update below code in your controller class
// Controller
public without sharing class getObjectNamesController {
public Map<String, Schema.SObjectType> object_Map = Schema.getGlobalDescribe();
public Set<String> objNamesSet = object_Map.keyset();
public List<SelectOption> objNamesList{
get{
List<SelectOption> options = new List<SelectOption>();
Integer i=0;
for(String myObjName : objNamesSet)
{
options.add(new SelectOption(String.valueOf(i),myObjName)) ;
i++;
}
System.debug(objNamesList);
return options;
}
set;
}
}
Let me know for any issues.
Thanks
Arpit
Please mark this answer as SOLVED and BEST ANSWER if it helps you.
Thanks
Arpit
Please mark this answer as SOLVED and BEST ANSWER if it helps you.