I'm still relatively new to lightning and APEX, and I've read (it seems like) 100s of posts / examples of how to do this, because it seems like a lot of people ask this, but I cannot make it work. I don't know anything about VisualForce and APEX programming, so those examples are difficult for me to understand and adapt:
use case: get a list of object field names to use in a lightning duallistbox. The object name will be passed in as a parameter.
Here is my latest iteration:
public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public String objName {get; set;}
public String selectedField {get; set;}
Public RapidiListConfigController()
{
objName = 'InvoiceHeader__c';
}
public List<SelectOption> getObjectNames()
{
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;
}
public Map<String, Schema.SObjectField> getObjectFields(String objName)
{
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType ObjectSchema = schemaMap.get(objName);
Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
List<SelectOption> fieldNames = new List<SelectOption>();
for (String fieldName: fieldMap.keySet())
{
fieldNames.add(new SelectOption(fieldName,fieldName));
//fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
}
return fieldMap;
}
But I cannot seem to get the fieldMap or fielNames list back to the component's js controller.
Thanks in advance.
static
or@AuraEnabled
. That's going to be your first step to make them visible to your Aura component. The second issue you'll encounter will be withList<SelectOption>
. TheSelectOption
object isn't aura enabled so you will get an error if you try to return it from a method. You can either create a new object that acts just likeSelectOption
or you can return aMap<String, String>
. Thelightning:dualListBox
expects an array of objects with label and value properties so you will need to build with that in mind.