Activity Forums Salesforce® Discussions How can I get all the Fields of an sObject in Salesforce apex class without hardcoding? Reply To: How can I get all the Fields of an sObject in Salesforce apex class without hardcoding?

  • PRANAV

    Member
    October 24, 2016 at 6:16 am

    Hi Mohit,

    You can use this piece of code

    fieldNameToString = new List<String>();
    fieldNames = new List<SelectOption>();
    String typeName = 'Case';
    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    Schema.SObjectType ObjectSchema = schemaMap.get(typeName);
    Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();

    for(String fieldName: fieldMap.keySet()) {
    fieldNames.add(new SelectOption(fieldName,fieldName));
    fieldNameToString.add(fieldName);
    }
    System.debug('all fields' + fieldNames);
    System.debug('fieldNameToString' + fieldNameToString);

    Hope this helps you.

    Thanks