• #16764
    Salesforce | Mohit Mohit #434
    Forcetalks

    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

    Tagged: , , ,

    #16772
    Salesforce | PRANAV PRANAV #15
    Forcetalks

    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

    #21631
     saloni gupta #180
    Forcetalks

    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);

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

Please to reply to this topic.

CONTACT US

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

Sending

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

or    

Forgot your details?