Apex Security and Sharing in Salesforce
Author
May 1, 2023
Salesforce provides a robust security model to protect data, ensuring that users have access only to the data they need. Apex, Salesforce’s proprietary programming language, follows this model to enforce security and sharing rules. Understanding Apex security best practices is crucial for developing secure and compliant applications.
Key Aspects of Apex Security
Enforcing User Permissions
Apex operates in system context, meaning it runs with elevated privileges by default. However, developers should enforce security checks to prevent unauthorized data access.
- With Sharing vs. Without Sharing:
with sharing
ensures that record-level security is respected.without sharing
runs Apex code with full system privileges.
Example:
public with sharing class AccountHandler { public List<Account> getUserAccounts() { return [SELECT Id, Name FROM Account]; } }
Field-Level and Object-Level Security
Even with sharing settings, users may not have permission to view certain fields or objects. Apex code must respect these permissions using:
- Schema methods
- Security.stripInaccessible() (introduced in API v45.0)
- Example:
public List<Account> getAccessibleAccounts() { return [SELECT Id, Name FROM Account WHERE Schema.sObjectType.Account.isAccessible()]; }
public List<Account> getAccessibleAccounts() { return [SELECT Id, Name FROM Account WHERE Schema.sObjectType.Account.isAccessible()]; } -
Using stripInaccessible: SObjectAccessDecision decision = Security.stripInaccessible(AccessType.READABLE, [SELECT Id, Name FROM Account]); List<Account> accessibleAccounts = (List<Account>)decision.getRecords();
CRUD and FLS Enforcement
Apex does not automatically enforce CRUD (Create, Read, Update, Delete) and FLS (Field-Level Security). Always validate access before querying or modifying records.
if (Schema.sObjectType.Account.isUpdateable()) {
Account acc = [SELECT Id FROM Account LIMIT 1];
acc.Name = ‘Updated Name’;
update acc;
}
Apex Sharing Mechanisms
Record-Level Sharing
Record sharing in Salesforce is determined by:- Organization-Wide Defaults (OWD)
- Role Hierarchies
- Sharing Rules
- Manual Sharing
- Apex Sharing
Apex Managed Sharing
If declarative sharing is not sufficient, Apex Managed Sharing allows programmatic record access using the Share
object.
Example:
AccountShare accShare = new AccountShare();
accShare.AccountId = ‘001XXXXXXXXXXXXXXX’;
accShare.UserOrGroupId = ‘005XXXXXXXXXXXXXXX’;
accShare.AccessLevel = ‘Read’;
insert accShare;
Using UserInfo for Context-Aware Access
Retrieve the current user’s ID:
Id currentUserId = UserInfo.getUserId();
Check profile or role:
String userProfile = [SELECT Profile.Name FROM User WHERE Id = :currentUserId].Profile.Name;
Best Practices for Apex Security
- Always use
with sharing
unless explicitly required. - Utilize
Security.stripInaccessible()
to enforce FLS. - Check CRUD permissions before DML operations.
- Avoid hardcoded IDs for sharing rules.
- Regularly audit security settings using tools like Salesforce Health Check.
Conclusion
Security is a crucial aspect of Apex development in Salesforce. By enforcing user permissions, adhering to record-sharing rules, and following best practices, developers can build secure applications that align with Salesforce’s robust security framework.
Pranshu Goyal, Director of Products at Mirekta, states: “We envision DSM to be used by every small to a medium-sized organization dealing with bad data and want to get rid of duplicates easily with no cost. We have faced issues dealing with duplicates in our organization. That inspired us to make a solution that is not only simple to use but can be used widely to make the organization’s data clean to make them more efficient and productive. We want DSM to be a solution for every organization looking for duplicate management capability better than the Salesforce out-of-the-box solution with no additional cost.”
Recent Posts
-
Leveraging AI to Enhance Sales Effectiveness13 Mar 2025 Use-case
-
Revolutionizing Manufacturing with AI: Predictive Maintenance, Supply Chain Optimization, and More11 Mar 2025 E-Book
-
NetSuite for Manufacturing: Streamlining Operations and Solving Key Challenges07 Mar 2025 Blog
-
How to Build Your First Agent in Salesforce Agentforce24 Feb 2025 Blog
-
ERP vs Salesforce Revenue Cloud: Which One is Right for Your Business?24 Feb 2025 E-Book
-
Revolutionizing Manufacturing with Salesforce: A Playbook for Efficiency & Growth18 Feb 2025 E-Book
-
Salesforce 2025 Game-Changing Trends You Need to Know28 Jan 2025 Blog
-
Agentforce 2.0: Everything You Need to Know About the Latest Update22 Jan 2025 Blog
-
The Ultimate Guide to NetSuite Development: Tools and Techniques10 Jan 2025 Blog
-
How Salesforce Nonprofit Cloud Transforms Fundraising Strategies10 Jan 2025 Blog
-
The Impact of Salesforce Development Partners on Small and Medium Businesses08 Jan 2025 Blog
-
Key Questions to Ask When Hiring a NetSuite Development Partner08 Jan 2025 Blog
-
Salesforce Agentforce Demystified: Your Essential Guide08 Jan 2025 Blog
-
Salesforce and NetSuite Integration: Driving Business Efficiency with Precision06 Jan 2025 Blog
-
Everest Group has positioned Mirketa as an Aspirant in the report24 Dec 2024 Press Release
-
Salesforce Einstein20 Dec 2024 E-Book
-
Order to Cash Cycle with NetSuite20 Dec 2024 E-Book
-
Empower Your Marketing Strategy with Salesforce Marketing Cloud's Automation Studio Activities13 Dec 2024 Blog
-
Salesforce CPQ for Subscription-based Businesses10 Dec 2024 Blog
-
Unleashing the Magic of Einstein Prediction Builder10 Dec 2024 Blog
Categories
Featured by



