Sub topic 13
Checking Access with Schema Describe Methods
This chapter covers describe checks, the older way to test field access in Apex. It assumes you know that Apex runs in system mode by default and that you can write basic SOQL and DML. Both points were covered in earlier chapters.
What describe checks do
Apex describe methods return metadata about objects and fields. Several of them answer a security question for the current user:
| Method | Question it answers |
|---|---|
| `isAccessible()` | Can the user read this field? |
| `isCreateable()` | Can the user set this field on a new record? |
| `isUpdateable()` | Can the user change this field on an existing record? |
You call them on a field's describe result, which you reach through Schema.sObjectType. The check runs against the current user's field-level security (FLS), even though the surrounding Apex runs in system mode.
A worked example
This method updates a field only when the user is allowed to edit it. It then reports what happened.
public with sharing class AccountRevenueService {
public static Boolean updateRevenue(Id accountId, Decimal revenue) {
if (!Schema.sObjectType.Account.fields.AnnualRevenue.isUpdateable()) {
return false;
}
Account acc = new Account(Id = accountId, AnnualRevenue = revenue);
update acc;
return true;
}
}If the user's profile and permission sets give Read but not Edit on AnnualRevenue, the method returns false and no DML runs.
The flow below shows the decision.
Checking before reading
Use isAccessible() the same way before you return field values:
if (Schema.sObjectType.Account.fields.AnnualRevenue.isAccessible()) {
List<Account> rows = [SELECT Id, AnnualRevenue FROM Account LIMIT 10];
}For object-level checks, use Schema.sObjectType.Account.isAccessible(). Run it before you check any field on that object.
Common errors
- Checking one field and querying many. Each field needs its own check. A check on
Namesays nothing aboutAnnualRevenue. - Forgetting that checks are manual. Nothing enforces the result. If you skip the check on one code path, that path leaks data.
- Confusing the check with the query. A describe check does not change how a SOQL query runs. The query still returns every field you ask for, because system mode ignores FLS.
- Using
isUpdateable()for inserts. UseisCreateable()when you create records. The two permissions can differ.
When to choose describe checks
Describe checks give you full control. You decide what happens when access is missing: return an error, skip a field, or branch the logic. The cost is repetition, because you write a check for every field on every path.
For most new code, WITH USER_MODE or AccessLevel.USER_MODE is shorter and harder to get wrong. Describe checks remain useful when you want custom behavior instead of an exception, or when you maintain older code that already uses them.
Exercise
A method queries Account.AnnualRevenue in system mode and first calls Schema.sObjectType.Account.fields.Name.isAccessible(). The user cannot read AnnualRevenue. What happens, and how do you fix it?
Show answer
The check passes, because it tests Name, not AnnualRevenue. The query still returns AnnualRevenue, because system mode ignores FLS, so the value leaks. Fix it by checking Schema.sObjectType.Account.fields.AnnualRevenue.isAccessible(), or by using WITH USER_MODE on the query.