Intermediate 30 min read

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
  1. Schema.DescribeSObjectResult accountDescribe = Account.sObjectType.getDescribe(); — getDescribe() returns metadata about the Account object itself — not a specific record, but facts about the object type.
  2. (blank line)
  3. System.debug(accountDescribe.getLabel()); — logs "Account", the object's label.
  4. 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
  1. Schema.DescribeFieldResult industryField = — the same idea applies at the field level.
  2. Account.Industry.getDescribe(); — metadata about the Industry field, independent of any particular Account record's actual Industry value.
  3. (blank line)
  4. System.debug(industryField.getLabel()); — logs "Industry", the field's label.
  5. System.debug(industryField.getType()); — logs PICKLIST, its data type.
  6. 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.');
}
  1. if (Account.Industry.getDescribe().isUpdateable()) { — rather than assuming every user running this code has permission to update Industry, checks isUpdateable() first.
  2. acc.Industry = 'Retail'; — only runs if permitted.
  3. update acc; — saves the change.
  4. } else { — otherwise...
  5. System.debug('Current user cannot update the Industry field.'); — ...reacts gracefully instead of failing with a permissions error partway through.
  6. } — closes the if/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()

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Schema Describe Basics Quiz

1. What does a Schema describe call provide?

Log in to submit the quiz and save your score.

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.