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
Alvin TanAlvin Tan 

How do I get all field names for an object in APEX?

Hi, I'm struggling with getting ALL of the field names for an object in APEX. When running the code below (in APEX), I am getting most of the fields but it's omitting a few contact fields as well as the comment field. When I run the block of code in anonymous apex, all the fields I need are returning. Is there something I might be missing? Thanks in advanced.
 
Schema.SObjectType targetType = Schema.getGlobalDescribe().get('case');
Map<String, Schema.SObjectField> fieldMap = targetType.getDescribe().fields.getMap();
    for (Schema.SObjectField field : fieldMap.values()) {
         System.debug(field.getDescribe().getName());
     }

 
KrishnaAvvaKrishnaAvva
Hi Alvin,

You wont be able to retrieve the relation fields on the object (lookup etc) using the above methods. It retrives only the standard/custom fields directly present on the object. Please clarify if these are relational fields which are missing?

Regards,
Krishna Avva
Alvin TanAlvin Tan
Hi, Thank you for the quick reply.

I believe you are right that they are relational fields. Would you happen to know a way to dynamically get both relationtional as well as standard/custom fields together? Thanks!
Raj VakatiRaj Vakati
Use this code

Refer this llink 

https://rajvakati.com/2018/10/17/lightning-component-clone-with-related-records/
 
public class SuperCloneService {
    public Id doClone(String parentId) {
        Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Set <String> fieldMap = schemaMap.get('Account').getDescribe().fields.getMap().keySet();
        List<String> finalFields = new List<String>() ;
        finalFields.addAll(fieldMap);
        SObjectType objToken = Schema.getGlobalDescribe().get(objectAPIName); 
        DescribeSObjectResult objDef = objToken.getDescribe();
        Map<String,String> so = new Map<String,String>();
        Map<String,String> so1 = new Map<String,String>();
        for (Schema.ChildRelationship cr: objDef.getChildRelationships()) 
        {
            if(cr.getField().getDescribe().isAccessible()&& cr.getField().getDescribe().isCreateable()&&cr.getField().getDescribe().isAccessible() && cr.getRelationshipName()!=null){
                if(querySobject.contains(''+cr.getChildSObject())){
                    so.put(''+cr.getChildSObject()  , ''+cr.getRelationshipName());
                    so1.put(''+cr.getRelationshipName()  , ''+cr.getField());
                }
            }
        } 
        
        List<String> subqueries = prepareSubqueries(so, schemaMap);
        String query =
            'SELECT ' + String.join(finalFields, ',')+
            ','+String.join(subqueries, ',') +
            ' FROM ' +objectAPIName +
            ' WHERE Id = \''+parentId+'\'';
        
        List<Sobject> parentObj = Database.query(query);
         
        
    }
    
    private List<String> prepareSubqueries(
        Map<String , String> childrelatedListObjects,
        Map <String, Schema.SObjectType> schemaMap
    ){
        List<String> subqueries = new List<String>();
        for(String childObject : childrelatedListObjects.keySet()){
            List<String> childFields = new List<String>();
            Map <String, Schema.SObjectField> fieldMap = schemaMap.get(childObject).getDescribe().fields.getMap();
            System.debug('fieldMap'+fieldMap);
            for(Schema.SObjectField sof : fieldMap.values()){
                DescribeFieldResult dfr = sof.getDescribe();
                if(dfr.isCreateable()){
                    childFields.add(dfr.getName());
                }
            }
            if(!childFields.isEmpty()){
                String query = '(SELECT ' + String.join(childFields, ',') + ' FROM ' + childrelatedListObjects.get(childObject) + ')';
                subqueries.add(query);
            }
            
        }
        return subqueries;
    }
    
}

 
DY DharamDY Dharam

Hi Alvin,

I hope this will help you to get the fileds name from the Object name:

String SobjectApiName = 'Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
for(String fieldName : fieldMap.keyset() )
    {
        system.debug('fieldName->>'+fieldName); // This will give you the api name of the field name.
    }

Thanks