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
Vireak RenVireak Ren 

how to get all objects and fields

How to get All Objects and Their Field? Please help me thank before<.^.>
Best Answer chosen by Vireak Ren
sachinarorasfsachinarorasf
Hi Vireak,

I have gone through your problem. Please try the below code.
 
List < Schema.SObjectType > gd = Schema.getGlobalDescribe().Values();      
Map<String , Schema.SObjectType > globalDescription = Schema.getGlobalDescribe();   
        
for ( Schema.SObjectType f : gd ) {  
  
    Schema.sObjectType objType = globalDescription.get(f.getDescribe().getName() );  
    Schema.DescribeSObjectResult r1 = objType.getDescribe();   
    Map<String , Schema.SObjectField > mapFieldList = r1.fields.getMap();    
  
    for ( Schema.SObjectField field : mapFieldList.values() ) {    
      
        Schema.DescribeFieldResult fieldResult = field.getDescribe();    
  
        if ( fieldResult.isAccessible() ) {    
          
            System.debug('Field Name is ' + objType + '.' + fieldResult.getName() );  
              
        }   
          
    }  
      
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Vireak,

I think you can use metadata api to get all the objects. You need to just write a package.xml which you can find in the developer guide.

Please find below the link to metadata api developer guide where package.xml for objects.

https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/manifest_samples.htm

You will have a tag named as field under respective object files.

In case if helped you I would request you to please close the thread by marking this as the best answer so that it can be used by others in the future and also helps in keeping the community clean.

Regards,
Anutej
sfdcBahusfdcBahu
One way would be use one of the Free Appexchange products to dump the fields in to Excel or create a report. there are multiple out there. Search in Appexchange for 'data model extract' or 'Fields extract'. 
sachinarorasfsachinarorasf
Hi Vireak,

I have gone through your problem. Please try the below code.
 
List < Schema.SObjectType > gd = Schema.getGlobalDescribe().Values();      
Map<String , Schema.SObjectType > globalDescription = Schema.getGlobalDescribe();   
        
for ( Schema.SObjectType f : gd ) {  
  
    Schema.sObjectType objType = globalDescription.get(f.getDescribe().getName() );  
    Schema.DescribeSObjectResult r1 = objType.getDescribe();   
    Map<String , Schema.SObjectField > mapFieldList = r1.fields.getMap();    
  
    for ( Schema.SObjectField field : mapFieldList.values() ) {    
      
        Schema.DescribeFieldResult fieldResult = field.getDescribe();    
  
        if ( fieldResult.isAccessible() ) {    
          
            System.debug('Field Name is ' + objType + '.' + fieldResult.getName() );  
              
        }   
          
    }  
      
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
This was selected as the best answer
Vireak RenVireak Ren
Okay, Thank you very much all guy
And I have a new one Question . I try to set variable but i still error" Variable does not exist: SObj.SObjectType "
But When change ''Schema.DescribeSObjectResult R = SObj.SObjectType.getDescribe();'' to "Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();" It's worked

Here is my Code 
String SObj = 'Account';
   Schema.DescribeSObjectResult R = SObj.SObjectType.getDescribe();
    	for(Schema.ChildRelationship cr: R.getChildRelationships()){
      		system.debug('====child object===='+cr.getChildSObject());            
	}
Thanks you again >*<