Skip to main content

Object-Level & Field-Level Security (CRUD/FLS)

 Object-Level & Field-Level Security (CRUD/FLS)

💬 In plain words:  Two locks on data: object-level (can you touch Accounts at all — Create/Read/Update/Delete) and field-level (okay, but can you see the Salary field?). Sharing decides WHICH records; CRUD/FLS decides WHAT you can do with the object and its fields.

📌 Example:  Asha can see the Candidate object (object-level ✓) but the Salary field is hidden from her profile (field-level ✗). She opens the record fine — the Salary column is simply not there, even in reports and the API.

Concept

CRUD (object permissions) and FLS (field permissions) are granted via profiles/permission sets and are enforced automatically in the standard UI, standard controllers, and Lightning Data Service — but NOT automatically in Apex, which runs in system mode by default on API v66 and earlier (from v67, user mode is the default — see 3.7). In older code Apex must opt in: SOQL with WITH USER_MODE, DML with 'as user', or Security.stripInaccessible() to sanitize records. This is distinct from record-level sharing (Module 3) — CRUD/FLS answers 'which objects/fields', sharing answers 'which rows'. Directly reused in Module 11.3 (secure SOQL) and Module 9 (LDS enforces FLS for LWC).

🧠 CRUD/FLS vs Sharing:  CRUD/FLS = WHICH objects & fields. Sharing = WHICH rows. Apex runs in SYSTEM mode → you must opt in (USER_MODE / stripInaccessible).

Core Q&A

Q: How do you enforce CRUD/FLS in Apex, and which mechanism do you choose when?

🎯 Say this first:  Use WITH USER_MODE in SOQL/DML as the default; Security.stripInaccessible for cleaning data; manual describe checks only for special cases.

A: Prefer user-mode database operations: 'SELECT ... WITH USER_MODE' and 'insert as user records' — they enforce CRUD, FLS, and sharing together and report all violations. WITH SECURITY_ENFORCED is the older query-only option; it throws on the first violation, ignores polymorphic fields, and does nothing for DML. Security.stripInaccessible(AccessType.READABLE/CREATABLE, records) is the graceful-degradation tool — instead of throwing, it strips fields the user cannot access, which is right for integration-facing or bulk paths where partial success is acceptable. Rule of thumb: user mode as the default in new code, stripInaccessible when you must not throw, SECURITY_ENFORCED only in legacy code you are not refactoring — and note it is REMOVED at API v67+: it no longer compiles (3.7).

// Modern default

List<Case> cases = [SELECT Id, Subject FROM Case

                   WHERE Status = 'Open' WITH USER_MODE];

 

// Graceful degradation

SObjectAccessDecision d = Security.stripInaccessible(

    AccessType.READABLE, cases);

return d.getRecords();

Follow-ups (scenario-based)

Q1: The standard UI hides a field from a user, but your LWC calling an Apex @AuraEnabled method still shows it. Why, and what are the two distinct fixes?

A1: Because the Apex method runs in system mode: FLS was checked by the page layout, not by your query. Fix one: make the Apex enforce security (WITH USER_MODE or stripInaccessible before returning). Fix two: eliminate the custom Apex read path and use Lightning Data Service / getRecord wire, which enforces FLS and CRUD for free and gives you caching (ties into Module 9.6). An architect answer notes the second option is preferred when no complex server logic is needed.

Q2: A configurable approval engine lets business users change rules without engineering. How do you stop that from becoming a privilege-escalation path?

A2: Two layers. First, config objects carry their own CRUD/FLS, so only the 'Approval Config Author' permission set can write rules. Second, the engine's evaluation code is designed carefully: rule evaluation reads config 'without sharing' (rules must apply globally), but every action it takes on the target record is executed in user mode. An approver can never be made to see or edit fields their own FLS forbids. That is, the engine amplifies process authority, never data authority. Stating that separation crisply is a strong architect-round moment.

Popular Posts

Must-listen songs for developers

Here are some must-listen songs for developers: "Strobe" by deadmau5 . This electronic dance music (EDM) track is perfect for getting into a flow state. The repetitive beat and simple melody are easy to focus on, and the overall mood of the song is upbeat and motivating.  "Viva la Vida" by Coldplay . This rock song has a soaring melody and powerful lyrics that can inspire you to stay focused and productive. The song's message of hope and resilience is perfect for those times when you're feeling stuck or discouraged.  "Code Monkey" by Jonathan Coulton . This tongue-in-cheek song is a hilarious and accurate portrayal of the life of a software developer. The lyrics are catchy and the song's upbeat tempo will make you want to get up and dance.  "The Sound of Silence" by Simon & Garfunkel . This classic folk song is perfect for those times when you need to focus and concentrate. The song's slow tempo and haunting melody will h...

Apex Test Class Examples for @HttpPost Exposed WebService Class

Introduction: In Salesforce, the Apex programming language allows you to create powerful web services that can be exposed to external systems for data integration. One common scenario is using the @HttpPost annotation to create a custom RESTful web service. In this blog post, we'll walk through some examples of how to write effective test classes for an @HttpPost exposed web service class in Salesforce. Writing comprehensive test classes ensures that your code is robust, functional, and ready for deployment.

Salesforce LWC Code for Multi-Select Lookup

Introduction: In Salesforce Lightning Web Components (LWC), implementing a multi-select lookup field can enhance the user experience and provide greater flexibility for selecting multiple related records. In this blog post, we will walk through the process of creating a multi-select lookup field using LWC. We will cover the required code snippets and provide step-by-step instructions to help you implement this functionality in your Salesforce org.

Uninstall all Windows 10 default apps using Powershell

Here is script to uninstall all windows 10 default modern apps. This script uninstalls xbox, xbox Game bar, Xbox App,Xbox Gaming Overlay, Get started etc from your computer. No need to run one by one commands Just copy below script, run  powershell as administrator and paste script and press enter . It will automatically uninstall all default programs.  If you do not  want to uninstall some apps than just remove " "  line from script. $packages = @( "7EE7776C.LinkedInforWindows" "C27EB4BA.DropboxOEM" "Microsoft.3DBuilder" "Microsoft.Microsoft3DViewer"

How to Save Quote PDF, Send PDF, Preview PDF in salesforce with custom functionality

Want to develop custom pdf viewer, save pdf in quote pdf related List and Send quote to customer on button click when quote is custom in salesforce . These functionality are standard from salesforce. but you can develop these functionality custom in salesforce. Here is the solution:- Custom button to save Quote PDF and send PDF  Step 1:-  First Create Two custom button. which will used for PDF preview and Save quote pdf in quotes pdf related list.                               1. PDF preview Button                              2. Save & Send Quote Button Replace "Your VF page here" to Your quote PDF cuatom page. Step 2:-  PDF preview button   pdf preview button will display the pdf's preview in standard format of salesforce. So you need to set the  following configuration (In picture). After that you have ...

Setup vs Non-Setup Objects (Mixed DML)

Core Platform & Fundamentals Module map MODULE 1 root: 'Who can do what, on which objects/fields?' ├─ Setup vs non-setup (transaction rules) ├─ Profile + Perm Sets + PSG (access = union) ├─ Licensing (the ceiling) ├─ CRUD/FLS (objects & fields) └─ Reports/Dashboards (who sees what data) 1.1 Setup vs Non-Setup Objects (Mixed DML) 💬 In plain words:   Salesforce keeps 'admin' records (like User, Group) and 'business' records (like Account, Case) in two separate rooms. One transaction cannot write to both rooms at once — that error is Mixed DML. The fix: do the second write in a separate async step. Concept

Unveiling the Power of Named Credentials in Salesforce with Comprehensive Code Examples

Introduction: Named Credentials are a powerful feature in Salesforce that allow you to securely authenticate and connect to external services and APIs without exposing sensitive information like usernames and passwords. In this blog post, we'll delve into the world of Named Credentials, understand their significance, and provide you with in-depth code examples to illustrate their implementation in various scenarios.