Skip to article
← BACK TO DISPATCH

Three Ways to Enforce Field-Level Security in Apex, and When WITH SECURITY_ENFORCED Will Burn You

One of the quietly dangerous assumptions in Apex development is that adding a security clause to a query means your data access is now protected. The clause is there. It says "enforced." Surely you are safe.

You are not necessarily safe. And the way you find out is not fun.

I have been in that moment: a query that ran perfectly in sandbox, passed every test, sailed through code review, and then threw unexpected exceptions in production for a specific subset of users. Not for everyone. Not on every run. Just for users whose profiles restricted access to one or more of the fields I had queried. In my own environment, where I had full permissions, the clause never triggered once. The code looked clean because, for me, it was clean. I had no idea it was waiting to fire on someone else.

That experience is what I want to save you from. So let me walk through the three approaches for enforcing field-level security in Apex, what each one actually does, and why the most obvious one can be the most dangerous.

The Three Mechanisms and What They Promise

Field-level security exists because not every user in an org should see every field. A standard user might not need to see compensation data. A partner community user definitely should not see certain internal notes. The platform has a permission model for this, and Apex, by default, bypasses it entirely. An Apex class runs with elevated privileges unless you do something about it.

There are three ways to do something about it.

The first is the describe-based approach. You interrogate the schema at runtime, check whether the running user has read access to a given field, and then construct your logic around what comes back. This has been available since the early days of the platform. It is explicit and granular; you check exactly what you want to check, when you want to check it. The downside is that it is verbose and easy to get wrong. Miss a field and it is a gap. It also does not age well as your object model grows, because every new field is something you have to remember to check.

The second is the SOQL clause approach, which is what WITH SECURITY_ENFORCED is. You add those words to a query and the platform evaluates field-level and object-level permissions for the running user before executing it. It feels elegant. It feels like the platform is doing the hard work for you. This is the approach that gets developers into trouble, but not for the reason most assume.

The third, and the one I reach for first now, is the stripInaccessible method and the related user mode execution. These either remove inaccessible fields from a collection of records after retrieval, or run the entire database operation in the context of the running user's permissions from the start. User mode, available on the platform more recently, enforces both field-level and object-level access across the board. It composes cleanly with patterns the older approaches do not, and it covers DML operations, not just queries.

Where WITH SECURITY_ENFORCED Quietly Burns You

Here is the part that matters, and the part that took me longer than it should have to learn. When you use WITH SECURITY_ENFORCED and the running user does not have access to a field you included in the query, the platform does not adapt or trim the results gracefully. It throws a QueryException and the operation stops entirely.

That sounds like exactly what you want from an enforcement mechanism. Fail loud. Stop the execution. But here is where the burn comes in.

In your sandbox, with your administrator or developer permissions, the clause never fires. You have access to everything. The query succeeds every time. The tests pass. The code review looks fine because the clause is right there, doing its job. You ship it.

Then in production, a user with a restricted profile runs the same code path. They do not have access to one field you included in the query. The platform throws. Your code, which was written expecting that query to succeed, does not handle that exception cleanly. And now you have a broken experience for a specific subset of users that you cannot reproduce on your own machine, because the clause only bites the people who lack the permissions you assumed everyone would have.

This is not a theoretical edge case. It is the predictable outcome of a mismatch between the permissions in your development environment and the actual permission landscape of your user base. The clause is doing exactly what it is supposed to do. The problem is that enforcement in the form of an exception, thrown inside code written for a full-access context, produces failures that are invisible to the developer and very visible to the user.

That was the insight that changed how I think about this. The clause is not too permissive. It is too strict in a way that punishes calling code that has not been written for the failure case. Once I understood that, the debate between the three tools became a question I could actually answer, rather than a set of options that all seemed roughly equivalent.

Compare WITH SECURITY_ENFORCED to stripInaccessible. When you use that approach, the platform removes inaccessible fields from the returned records and hands you back the modified collection without throwing. You can inspect what was stripped. You can log it. You can write tests that assert a given profile should not see a given field and then verify the mechanism caught it correctly. The contract is observable and the operation completes. Your code continues with the data the user is allowed to see, and you handle the case where something was removed deliberately, on your terms, not on the platform's.

User mode execution takes a different angle. It enforces field-level and object-level permissions from the start, and it throws when access is missing, but you write your code knowing the exception is coming and you handle it intentionally. The failure surface is designed for, not stumbled into.

That distinction is what the burn comes down to: a failure your code was written to expect is a feature. A failure your code was not written to expect is a production incident.

Choosing the Right Tool

The practical guidance is straightforward once you know the landscape.

If you need the strictest enforcement with intentional failure handling, user mode execution is the right starting point. It covers the widest surface, fails predictably, and gives you an explicit signal to catch and handle. Your code owns the failure case from the beginning.

If you are working with record collections and need to guarantee inaccessible fields are not exposed or written during DML, stripInaccessible gives you observability and control in the same operation. The inaccessible fields are removed, you can see exactly what was removed, and the operation completes without stopping. You handle the reduced result set on purpose.

If you have a query that is specifically intended to throw when a user lacks access to any of the queried fields, and you have written the calling code to catch and handle that QueryException properly, then WITH SECURITY_ENFORCED can serve that purpose. It does exactly what it says. The problem is reaching for it as a general-purpose safety clause in code that was not written for the exception case. Document it. Test it explicitly with restricted-permission user contexts rather than assuming your sandbox credentials represent the production reality.

The describe-based approach is largely superseded for most use cases. It still has a place in highly conditional logic where you need field-level checks before constructing dynamic queries at runtime, but as a blanket strategy it trades brevity and maintainability for granularity that is rarely needed.

What I keep returning to is that security in Apex is not a checkbox. It is not add-this-clause-and-you-are-done. The platform gives you tools with meaningfully different contracts, and choosing between them requires knowing those contracts. Most of the pain I have seen in this space comes not from developers ignoring security, but from developers reaching for the most visible solution without reading the fine print on what it actually promises.

I paid that cost in production, on a Thursday afternoon, debugging exceptions I could not reproduce on my own machine. Now I read the fine print before shipping. The clause is the beginning of the decision, not the end of it.

Get the next dispatch when it drops.

SUBSCRIBE FOR THE NEXT DROP MORE ARTICLES