Apex Class:
- public class FieldListController
- {
- public static void fetchFields(String objectName)
- {
- List<SelectOption> fieldList = new List<SelectOption>();
- Map<String , Schema.SObjectType> describeSObject = Schema.getGlobalDescribe();
- Schema.sObjectType objType = describeSObject.get(objectName);
- if(objType != null)
- {
- Schema.DescribeSObjectResult desResult = objType.getDescribe();
- Map<String, Schema.SObjectField> fieldMap = desResult.fields.getMap();
- for(Schema.SObjectField currField : fieldMap.values())
- {
- Schema.DescribeFieldResult fieldResult = currField.getDescribe();
- if(fieldResult.isAccessible())
- {
- fieldList.add(new SelectOption(fieldResult.getLabel(), fieldResult.getLabel()));
- }
- }
- System.debug('### Field List Controller'+fieldList);
- }
- }
- }
Run From Developer Console:
Pass the object API Name in the below method,
FieldListController.fetchFields('Account');
No comments:
Post a Comment