You can fetch all sobject and their field information in apex by using describe methods. Apex gives 2 data types for sobject and field information.
On the describe result, the getSObjectType and getSObjectField methods return the tokens for sObject and field, respectively.
Finding All object in Organization
Schema getGlobalDescribe method returns a map that represents the relationship between all sObject names (keys) to sObject tokens (values).
For example:
Map<String, Schema.SObjectType> schemaMapOfOrg = Schema.getGlobalDescribe();
The map has the following characteristics:
If we need to create contact record dynamically, then use below code:
String typeName='Contact';
Schema.SObjectType objToken = Schema.getGlobalDescribe().get(typeName);
sobject son= objToken.newSObject();
son.put('firstname','Execute1');
son.put('LastName','Kumar');
son.put('Email','sunil.kumar2@in.gmail.com');
insert son;
Apex code to find all sobject and their fields
Apex code to generate SOQL to fetch all fields of an object
- Token: A lightweight, serializable reference to an sObject or a field that is validated at compile time.
- Describe Result: It contains all the describe information about sobject and fields. Describe result objects are not serializable, and are validated at runtime.
On the describe result, the getSObjectType and getSObjectField methods return the tokens for sObject and field, respectively.
Finding All object in Organization
Schema getGlobalDescribe method returns a map that represents the relationship between all sObject names (keys) to sObject tokens (values).
For example:
Map<String, Schema.SObjectType> schemaMapOfOrg = Schema.getGlobalDescribe();
The map has the following characteristics:
- It is dynamic, that is, it is generated at runtime on the sObjects currently available for the organization, based on permissions.
- The sObject names are case insensitive.
- The keys use namespaces as required. The keys reflect whether the sObject is a custom object.
If we need to create contact record dynamically, then use below code:
String typeName='Contact';
Schema.SObjectType objToken = Schema.getGlobalDescribe().get(typeName);
sobject son= objToken.newSObject();
son.put('firstname','Execute1');
son.put('LastName','Kumar');
son.put('Email','sunil.kumar2@in.gmail.com');
insert son;
Apex code to find all sobject and their fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); | |
for(String ss1: schemaMap.keyset()){ | |
if(ss1.equalsignorecase('Contact')){ | |
Schema.SObjectType objToken=schemaMap.get(ss1); | |
//find details about sobject | |
Schema.DescribeSObjectResult objDescribe=objToken.getdescribe(); | |
system.debug('*********sobjectAPINamel:'+objDescribe.getName()); | |
system.debug('*********sobjectlabel:'+objDescribe.getlabel()); | |
system.debug('*********sobjectPrefix:'+objDescribe.getKeyPrefix()); | |
system.debug('*********IsCustom sobject:'+objDescribe.IsCustom()); | |
//finding all fields of sobjects | |
Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); | |
for(String ss:Fieldmap.keyset()){ | |
Schema.DescribeFieldResult fd=fieldMap.get(ss).getDescribe(); | |
system.debug('*********fieldAPINamel:'+fd.getName()); | |
system.debug('*********fieldlabel:'+fd.getlabel()); | |
system.debug('*********fieldtype:'+fd.gettype()); | |
system.debug('*********isNillable:'+fd.isNillable()); | |
//check if data type is picklist then find out picklist options | |
if(String.valueof(fd.getType()).equalsignorecase('Picklist')){ | |
List<Schema.PicklistEntry> Pp = fd.getPicklistValues(); | |
for(Schema.PicklistEntry p:Pp){ | |
system.debug('****picklist option label'+P.getLabel()); | |
system.debug('****picklist option value'+P.getvalue()); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String objectName = 'Lead'; | |
Schema.SObjectType objToken = Schema.getGlobalDescribe().get(objectName); | |
//find details about sobject | |
Schema.DescribeSObjectResult objDescribe=objToken.getdescribe(); | |
system.debug('*********sobjectAPINamel:'+objDescribe.getName()); | |
//finding all fields of Lead and generating query dynamically | |
Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); | |
String query = 'Select '; | |
string conid='00Qxxxxxxxxxx'; //you can hard code the LeadId or you can pass it through apex method | |
for(String ss:Fieldmap.keyset()){ | |
Schema.DescribeFieldResult fd=fieldMap.get(ss).getDescribe(); | |
system.debug('*********fieldAPINamel:'+fd.getName()); | |
query = query + fd.getName() + ','; | |
} | |
query = query.subString(0,query.length() - 1); | |
query = query + ' from '; | |
query = query + objDescribe.getName(); | |
query = query + ' where Id = \''; | |
query = query + conid + '\''; | |
system.debug('*********Lead Query: ' + query); |