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
Bharat KumarBharat Kumar 

Retreive object schema and related field api names using apex

Hi,

 

I would like to get the list of objects using apex class and also I need to get what are the fields for the respoective object using apex.

 

Is there any way to do this?

 

 

Thank you,

Bharat

souvik9086souvik9086

Retrieve Objects

 

public List<SelectOption> getObjectNames() {
      
        
            //Variables to hold the Obj Label and Obj LabelName map
            List<String> objLabels = new List<String>();
            Map<String, String> objLabelNameMap = new Map<String, String>();
            
            //To fetch all the objects of the current Organisation
            List<Schema.SObjectType> allObjects = Schema.getGlobalDescribe().Values();    
            List<SelectOption> objectOptions = new List<SelectOption>();
            objectOptions.add(new SelectOption('', '--None--));
            
            //Loop to iterate over all objects
            for(Schema.SObjectType objectName : allObjects) {   
            
                objLabels.add(objectName.getDescribe().getLabel());
                objLabelNameMap.put(objectName.getDescribe().getLabel(), 
                                                       objectName.getDescribe().getName());
            }
            objLabels.sort();
            for(String objLabel : objLabels){
                
                objectOptions.add(new SelectOption(objLabelNameMap.get(objLabel),objLabel));
            }
            return objectOptions;
    }
    

This will retrieve all the objects.

 

Retrieve fields of the selected object

 

public List<SelectOption> getObjectFields(String objectName) {
    
            Map<String, SObjectField> allFields = new Map<String, SObjectField>();
            
            //Fetching all the fields of the Selected Object
            allFields = allObjects.get(objectName).getDescribe().fields.getMap();  
            List<SelectOption> fieldOptions = new List<SelectOption>();
            fieldOptions.add(new SelectOption('', '--None--'));
            List<SObjectField> fieldTokens = new List<SObjectField>();
            
            //Fetching the field tokens
            fieldTokens = allFields.values();
            
            //Loop to iterate over the fieldtokens
            for(SObjectField fieldToken : fieldTokens) {
            
                DescribeFieldResult descFieldRes = fieldToken.getDescribe();
                
                fieldOptions.add(new SelectOption(descFieldRes.getName(), descFieldRes.getLabel()));
            }
            return fieldOptions;
    }

This will retrieve all the fields of the selected object.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Bharat KumarBharat Kumar
Hi souvik,

Thanks for your reply.

Here If I need to get only custom object information and their respective fields, what should I do
souvik9086souvik9086

Then modify like this

 

public List<SelectOption> getObjectNames() {
      
        
            //Variables to hold the Obj Label and Obj LabelName map
            List<String> objLabels = new List<String>();
            Map<String, String> objLabelNameMap = new Map<String, String>();
            
            //To fetch all the objects of the current Organisation
            List<Schema.SObjectType> allObjects = Schema.getGlobalDescribe().Values();    
            List<SelectOption> objectOptions = new List<SelectOption>();
            objectOptions.add(new SelectOption('', '--None--));
            
            //Loop to iterate over all objects
            for(Schema.SObjectType objectName : allObjects) {   
            
if(objectName.getDescribe().isCustom() == TRUE) { 
                 objLabels.add(objectName.getDescribe().getLabel());
                 objLabelNameMap.put(objectName.getDescribe().getLabel(), 
                                                       objectName.getDescribe().getName());
}
            }
            objLabels.sort();
            for(String objLabel : objLabels){
                
                objectOptions.add(new SelectOption(objLabelNameMap.get(objLabel),objLabel));
            }
            return objectOptions;
    }

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks