function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TeeJay BagainTeeJay Bagain 

Getting Field API Names Case-sensitive

I am trying to get the API Names of a field of a certain object.

For example, I have a field named Testing with an API name of Testing__c.

However when I use 
Set<String> fieldExistingCompare = new Set<String>();

Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get('Request__c').getDescribe().fields.getMap();
Then getting the values via loop like this...
for (String fieldAPINameIN: fieldMap.keySet())
{
              fieldExistingCompare.add(fieldAPINameIN);
}
I get testing__c instead. I need to get the actual API Name of the field with the proper capitalizations.

How do I achieve this? Some people have done it yet I still don't get it.

Your help is deeply appreciated.

Thank you!


Best Answer chosen by TeeJay Bagain
manhnt.bkitmanhnt.bkit
Hi TeeJay Bagain,

This is my example for Contact, it will return Field API Names Case-sensitive :

List<Schema.SObjectType> objects = new List<Schema.SObjectType>{ Contact.SObjectType};


    for(Schema.SObjectType objType: objects){
        for(Schema.SObjectField fld: objType.getDescribe().fields.getMap().values()){
            
            System.debug('API Field Name =  '+fld.getDescribe().getName());
        }
    }

Manh

All Answers

manhnt.bkitmanhnt.bkit
Hi TeeJay Bagain,

This is my example for Contact, it will return Field API Names Case-sensitive :

List<Schema.SObjectType> objects = new List<Schema.SObjectType>{ Contact.SObjectType};


    for(Schema.SObjectType objType: objects){
        for(Schema.SObjectField fld: objType.getDescribe().fields.getMap().values()){
            
            System.debug('API Field Name =  '+fld.getDescribe().getName());
        }
    }

Manh

This was selected as the best answer
TeeJay BagainTeeJay Bagain
Thank you Manh! Deeply appreciated it. :)