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

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

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.

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"

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.

Unleashing the Power of Flow Orchestrator in Salesforce: Streamlining Business Processes

Introduction: Salesforce Flow Orchestrator is a powerful tool that enables businesses to automate and streamline complex business processes within the Salesforce platform. With its intuitive visual interface, advanced workflow capabilities, and seamless integration with other Salesforce features, Flow Orchestrator revolutionizes how organizations manage and optimize their processes. In this blog post, we will explore the features, benefits, and potential of Flow Orchestrator in driving operational efficiency and productivity. 1. Simplifying Complex Business Processes: Flow Orchestrator empowers business users to design and execute complex workflows without the need for extensive coding or technical expertise. Its visual interface allows users to create dynamic flows, define decision points, and automate repetitive tasks, all while maintaining a clear and intuitive process design. 2. Enhanced Automation with Conditional Logic: Flow Orchestrator offers advanced conditional logic c...

Custom Footer Buttons in Screen Flow: LWC Code Examples

Introduction: Screen Flows are a powerful tool in Salesforce that allow you to create guided user experiences by presenting a sequence of screens to collect data or guide users through a process. While Salesforce provides default footer buttons in screen flows, there are scenarios where you might want to customize these buttons to meet specific requirements. In this blog post, we will explore how to create custom footer buttons using Lightning Web Components (LWC) in a screen flow.

Deploy Lightning Web Component Files - Full Code with Output

Introduction: Lightning Web Components (LWC) are a powerful way to build efficient and reusable components in the Salesforce ecosystem. In this blog, we will walk through the process of deploying Lightning Web Component files and explore the full code with the output. By the end of this tutorial, you will have a clear understanding of how to create, deploy, and test Lightning Web Components.