Activity › Forums › Salesforce® Discussions › What are some use cases for using the Schema class in Salesforce?
Tagged: Classes in Salesforce, Record ID, Record Type, Schema, SOQL
-
What are some use cases for using the Schema class in Salesforce?
Posted by Avnish Yadav on July 26, 2018 at 5:46 AMWhat are some use cases for using the Schema class in Salesforce?
Parul replied 7 years, 8 months ago 4 Members · 3 Replies -
3 Replies
-
hello Avnish
by Schema class you can retrieve data from server without SOQL query
for eg- get recordTypeId without soql query
Id contRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get(‘NameOfRecordType’).getRecordTypeId();
- [adinserter block='9']
-
Hi,
Here is the code which gives best use case of schema for getting fields –
public Void GetAllField()
{
String query =”;
String SobjectApiName = ‘Account’;
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();String strFields = ”;
for(String fieldName : fieldMap.keyset() )
{
if(strFields == null || strFields == ”)
{
strFields = fieldName;
}else{
strFields = strFields + ‘ , ‘ + fieldName;
}
}query = ‘select ‘ + strFields + ‘ from ‘ + SobjectApiName + ‘ Limit 10 ‘;
List <Account> accList = Database.query(query);
}
Hope this helps.
-
Contains methods for obtaining schema describe information. Schema Is data about data. SObjectType is used to check the object type. Pleas try below code it will give you list of object in your org
For ex:Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
List<String> entities = new List<String>(schemaMap.keySet());
For(String name : entities)
{
System.debug(‘—–>’+name);
}Thanks
Log In to reply.