Schema Describe Basics
By the end of this lesson, you'll be able to:
- Use Schema methods to inspect an object or field's metadata at runtime
- Check whether a field is accessible or updateable before using it
- Explain why describe calls are useful for building genuinely reusable Apex
Prerequisites: "Dynamic sObjects"
Describing an object
Schema.DescribeSObjectResult accountDescribe = Account.sObjectType.getDescribe();
System.debug(accountDescribe.getLabel()); // "Account"
System.debug(accountDescribe.isCreateable()); // true or false, depending on permissions
Schema.DescribeSObjectResult accountDescribe = Account.sObjectType.getDescribe();—getDescribe()returns metadata about theAccountobject itself — not a specific record, but facts about the object type.- (blank line)
System.debug(accountDescribe.getLabel());— logs"Account", the object's label.System.debug(accountDescribe.isCreateable());— logs whether the current running context is even allowed to create one.
Describing a field
Schema.DescribeFieldResult industryField =
Account.Industry.getDescribe();
System.debug(industryField.getLabel()); // "Industry"
System.debug(industryField.getType()); // PICKLIST
System.debug(industryField.isAccessible()); // true or false
Schema.DescribeFieldResult industryField =— the same idea applies at the field level.Account.Industry.getDescribe();— metadata about theIndustryfield, independent of any particularAccountrecord's actualIndustryvalue.- (blank line)
System.debug(industryField.getLabel());— logs"Industry", the field's label.System.debug(industryField.getType());— logsPICKLIST, its data type.System.debug(industryField.isAccessible());— logs whether it's currently readable.
Why this matters: checking access before using a field
if (Account.Industry.getDescribe().isUpdateable()) {
acc.Industry = 'Retail';
update acc;
} else {
System.debug('Current user cannot update the Industry field.');
}
if (Account.Industry.getDescribe().isUpdateable()) {— rather than assuming every user running this code has permission to updateIndustry, checksisUpdateable()first.acc.Industry = 'Retail';— only runs if permitted.update acc;— saves the change.} else {— otherwise...System.debug('Current user cannot update the Industry field.');— ...reacts gracefully instead of failing with a permissions error partway through.}— closes theif/else.
Genuinely reusable code that adapts to who's actually running it — a concept a later Security module builds on significantly.
Exercise
Write code that describes the Contact object and debugs its label using getLabel().
Show hint
Contact.sObjectType.getDescribe().getLabel()
Schema Describe Basics Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Schema describe calls let Apex inspect an object or field's own metadata at runtime — its label, its type, whether the current user can even see it — rather than assuming.