- SalesforceChaCha
- Posts
- π Salesforce Security Smackdown πΊ
π Salesforce Security Smackdown πΊ
Comparing the major security players in Apex
Good morning, Salesforce Nerds! And welcome to the ultimate showdown of data security! π₯
Today, we pit three contenders against each other in the ring of Apex:
π isAccessible()
π WITH USER_MODE
π WITH SECURITY_ENFORCED
If you've ever found yourself wondering which security method reigns supreme, buckle up - this is going to be a fun ride! π’

TABLE OF CONTENTS
Salesforce Security Smackdown
ROUND 1
The OG - isAccessible()
Our first contender has been around since the days when admins still believed Workflow Rules were the future. π
Schema.DescribeFieldResult.isAccessible()
is the granddaddy of field-level security enforcement in Apex.
How It Works:
Easy, really. Before querying or manipulating a field in Apex, you check βοΈ if the running user has access by invoking the isAccessible() method on it.
Like this ποΈ
if (Schema.sObjectType.Account.fields.Industry.isAccessible()) {
String industry = [SELECT Industry FROM Account WHERE Id = :accountId].Industry;
}
Pros:
β
Explicit, granular control over what you check
β
Works great when dynamically handling different fields
Cons:
β Manual checks make it easy to forget enforcement
β Tedious and verbose for large queries
β Still leaves you vulnerable if you forget field checks elsewhere in the logic
Think of isAccessible()
like an old-school bodyguard. π
Effective, but you have to tell them exactly who to check at the door.
ROUND 2
The Newcomer - WITH USER_MODE
Our second contender was GAβd in the Spring β23 Release!
The WITH USER_MODE
clause lets your SOQL queries automatically respect user permissions without extra code. πͺ
How It Works:
Just add WITH USER_MODE
to your query, and Salesforce handles the rest. β¨
Peep this ποΈ
Account acc = [SELECT Name, Industry FROM Account WHERE Id = :accountId WITH USER_MODE];
Pros:
β
Enforces object and field-level security automatically
β
Simplifies code - no more manual isAccessible()
checks
β
Works with DML (insert as user, for example)
Cons:
β Still a bit in its early days - some limitations exist
β Requires Apex code to run in user mode, so not useful for system processes
If isAccessible()
is a bouncer checking IDs at the door, WITH USER_MODE
is like a smart venue that only lets in guests who already passed security. π§βπ»
Smooth and effortless! ποΈ
ROUND 3
The Enforcer - WITH SECURITY_ENFORCED
The last contender, WITH SECURITY_ENFORCED
, has been enforcing security since Spring β20.
This bad boy automatically applies field - and object-level security to SOQL queries. π₯
How It Works:
Add WITH SECURITY_ENFORCED
to your query, and Salesforce ensures that only accessible fields are queried:
List<Account> accounts = [SELECT Name, Industry FROM Account WITH SECURITY_ENFORCED];
Pros:
β
Super easy to implement - just slap it onto your SOQL
β
Strict enforcement - throws an exception if a user lacks access
Cons:
β Throws errors instead of gracefully handling missing fields
β Can be too aggressive in some use cases
β Doesn't work with DML statements - SOQL only!
Think of WITH SECURITY_ENFORCED
as the nightclub bouncer who not only checks IDs but also tosses out anyone who doesnβt belong - no second chances! πͺ
FINAL VERDICT
Which One Should You Use?
So, which method wins? It depends on your use case!
Use
isAccessible()
when you need fine-grained, manual control over fields.Use
WITH USER_MODE
for an effortless, user-friendly experience.Use
WITH SECURITY_ENFORCED
when you want strict enforcement and can handle potential errors.
Either way, using any of these methods is better than ignoring security altogether - unless you want your org to be the next security horror story. π
Feature | isAccessible() | WITH USER_MODE | WITH SECURITY_ENFORCED |
---|---|---|---|
Security Level | β Good | β Better | β Best |
Ease of Use | β Tedious | β Very easy | β Simple |
Auto-Enforcement | β No | β Yes | β Yes |
Throws Errors | β No | β No | β Yes |
SOUL FOOD
Todayβs Principle
"Security is always excessive until itβs not enough."
and now....Salesforce Memes



What did you think about today's newsletter? |