Activity Forums Salesforce® Discussions 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

  • saloni gupta

    Member
    August 14, 2017 at 1:34 pm

    Hi Mohit,

    You can try this :-

    string fields = '';

    Map<String,Schema.SObjectField> fieldMap = Schema.SObjectType.Opportunity.fields.getMap();

    for(String field : fieldMap.keySet())

    {

    fields += field + ', ';
    }
    System.debug('fiedsOpportunity=='+fields);

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos