• #16764
    Salesforce | Mohit Mohit #478

    How can I get all the Fields of an sObject in Salesforce apex class without hardcoding?

    Hi All,

    How can I get all the Fields of an sObject in Salesforce apex class without hardcoding?

    please give suggestion


    Salesforce | PRANAV PRANAV #659

    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));
    System.debug(‘all fields’ + fieldNames);
    System.debug(‘fieldNameToString’ + fieldNameToString);

    Hope this helps you.


     saloni gupta #648

    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 + ‘, ‘;

Viewing 3 posts - 1 through 3 (of 3 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?