πŸ’ƒ 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

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."

Robbie Sinclair

and now....Salesforce Memes

What did you think about today's newsletter?

Login or Subscribe to participate in polls.