Schema Class in Salesforce Apex | The Developer Guide
INTRODUCTION:
Schema is a Namespace which is referred as Database. This namespace contains some classes and their methods to provide metadata information of Schema.
Schema class deals with both bulk and single record means you can fetch all objects' names at once or a single object name. Basically a schema class is used to make a dynamic application.
One of the best parts of Using Schema Class is you can query any object and there fields without using any Soql query.you can directly interact with the database using schema methods.
For example: ChildRelationship class contains methods which describe the number of child relationships and the child objects related to a particular parent object. Like this, there are so many classes and their methods available in Schema class which we can use according to our needs and purpose.
Don't forget to check out: Creating A Multi-Select Picklist As Checkbox In Salesforce Visualforce Pages
Consideration while using Schema Class:
All methods used in Schema class are Static, Some of them are as:
1. getGlobalDescribe()
-This method returns a map of all objects in org with names(keys) and tokens(values) for both Custom and Standard objects.This method is dynamic and it is generated at the run time of the currently available sObjects in the Org.
Syntax-
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
2. describeSObjects(sObjectTypes)
-This method returns a list of fields and properties of a particular sObject or array of sObjects.
Syntax-
Schema.DescribeSObjectResult[] Results = Schema.describeSObjects(new String[]{'Account','opportunity'});
3. describeDataCategoryGroups(sObjectNames)
-This method is used to return a list of categorized groups linked with a particular object or specified object.
Syntax-
public static List<Schema.DescribeDataCategoryGroupResult> describeDataCategoryGroups(List<String> sObjectNames)
4. describeTabs()
-This method is used to return information about Custom and Standard apps available to current users.
Syntax-
Schema.DescribeTabSetResult[] tabInfo = Schema.describeTabs();
Let's take an Example to display a list of all sObjects in the Visualforce page.
First we have to write an Apex Controller to fetch a list of all objects and after that use this Controller in the VF page to display a list of objects.
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; } }
Check out another amazing blog by Anuj here: Introduction To Collections In Apex | Salesforce Developer Guide
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 take another example to display a list of objects and their related fields. you can use this controller in both lightning component and 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; }
Thank you.