Opening Statement:
Schema is a designated namespace often referred to as the “Database.” This namespace encompasses several classes and their associated methods, which serve the purpose of providing metadata insights about the Schema.
The Schema class is versatile, accommodating both bulk and single-record operations. This means you can retrieve the names of all objects in one go or focus on a single object’s name. Essentially, a Schema class is instrumental in creating dynamic applications.
One of the most advantageous aspects of utilizing the Schema Class is that it grants you the ability to query any object and its fields without the need for explicit SOQL queries. You can directly interact with the database by leveraging Schema methods.
For instance, the ChildRelationship class within the Schema contains methods that offer information about the number of child relationships and the child objects associated with a specific parent object. In a similar vein, the Schema class encompasses numerous other classes and methods tailored to a variety of requirements and objectives.
Points to Keep in Mind When Using the Schema Class:
Every method employed in the Schema class is static. Here are a few examples:
1. getGlobalDescribe()
- This method provides a dynamic map containing names (as keys) and tokens (as values) for all objects in the organization, encompassing both Custom and Standard objects. The map is generated at runtime based on the currently available sObjects in the Org.
Syntax:
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
2. describeSObjects(sObjectTypes)
- This method furnishes a list of fields and attributes for a specific sObject or an array of sObjects.
Syntax:
Schema.DescribeSObjectResult[] Results = Schema.describeSObjects(new String[]{'Account','opportunity'});
3. describeDataCategoryGroups(sObjectNames)
- This method is employed to retrieve a list of categorized groups associated with a specified object or objects.
Syntax:
public static List<Schema.DescribeDataCategoryGroupResult> describeDataCategoryGroups(List<String> sObjectNames)
4. describeTabs()
- This method is utilized to provide details about Custom and Standard apps accessible to the current users.
Syntax:
Schema.DescribeTabSetResult[] tabInfo = Schema.describeTabs();
Let’s consider an example of displaying a list of all sObjects on a Visualforce page.
First, we need to create an Apex Controller to retrieve a list of all objects. Then, we can utilize this Controller within the Visualforce page to display the list of objects.
Here’s the Apex Controller:
public class GetListofAllObjects { Public string SelectObj{get;set;} Public Map<String, Schema.SObjectType> AllObjMap; Public GetAllObjectsListController(){ AllObjMap= New Map<String, Schema.SObjectType>(); AllObjMap= Schema.getGlobalDescribe(); } Public List<selectoption> getAllObjectList(){ List<selectoption> AllobjectList = new List<selectoption>(); for(string r:AllObjMap.keyset()){ AllobjectList.add(new selectoption(r,r)); } return AllobjectList; } }
Visualforce Page:
<apex:page controller="GetListofAllObjects"> <apex:form > <apex:pageBlock id="AllObjectsList"> <apex:outputlabel value="Object Name"/> <apex:selectList value="{!SelectObj}" id="ObjPickList" size="1"> <apex:selectOptions value="{!ObjectList}"/> </apex:selectList> </apex:pageBlock> </apex:form> </apex:page>
Let’s explore another example, this time to showcase a list of objects along with their associated fields. You can utilize this controller in both a Lightning component and a Visualforce Page.
Apex Controller:
public class SchemaController { @AuraEnabled public static List<String> getobjectNames(){ List<Schema.SObjectType> getdetail = Schema.getGlobalDescribe().Values(); List<String> selectOptions = new List<String>(); for(SObjectType ss : getdetail){ options.add(ss.getDescribe().getName()); options.sort(); } System.debug('selectOptions--->>' + selectOptions); return selectOptions; } @AuraEnabled public static List<String> getObjectFields(String selectedObject){ List<String> fields = new List<String>(); Map<String , Schema.SObjectType> globalDescript = Schema.getGlobalDescribe(); Schema.sObjectType objectTypess = globalDescript.get(selectedObject); Schema.DescribeSObjectResult ros1 = objectTypess.getDescribe(); Map<String , Schema.SObjectField> mapFieldLists = ros1.fields.getMap(); for(Schema.SObjectField field : mapFieldLists.values()) { Schema.DescribeFieldResult fieldResultss = field.getDescribe(); if(fieldResult.isAccessible()) { fields.add(fieldResultss.getName()); } } List<String> str = new List<String>(); for(String fstr : fields){ str.add(fstr); str.sort(); } System.debug('str---->>' + str); return str; }