Apex Security

Last updated on Nov 24 2021
Abha Kulkarni

Table of Contents

Apex Security

Apex security refers to the method of applying security settings and enforcing the sharing rules on running code. Apex classes have security setting which will be controlled via two keywords.

Data Security and Sharing Rules

Apex generally runs in system context, that is, the present user’s permissions. Field-level security, and sharing rules aren’t taken under consideration during code execution. Only the anonymous block code executes with the permission of the user who is executing the code.
Our Apex code shouldn’t expose the sensitive data to User which is hidden via security and sharing settings. Hence, Apex security and enforcing the sharing rule is most vital.

With Sharing Keyword

If you employ this keyword, then the Apex code will enforce the Sharing settings of current user to Apex code. This doesn’t enforce the Profile permission, only the info level sharing settings.
Let us consider an example wherein, our User has access to five records, but the entire number of records is 10. So when the Apex class are going to be declared with the “With Sharing” Keyword, it’ll return only 5 records on which the user has access to.
Example
First, confirm that you simply have created a minimum of 10 records within the Customer object with ‘Name’ of 5 records as ‘ABC Customer’ and rest 5 records as ‘XYZ Customer’. Then, create a sharing rule which can share the ‘ABC Customer’ with all Users. We also got to confirm that we’ve set the OWD of Customer object as Private.
Paste the code given below to Anonymous block within the Developer Console.

// Class With Sharing
public with sharing class MyClassWithSharing {
// Query To fetch 10 records
List CustomerList = [SELECT id, Name FROM APEX_Customer__c LIMIT 10];

public Integer executeQuery () {
System.debug('List will have only 5 records and therefore the actual records are' 
+ CustomerList.size()+' as user has access to'+CustomerList);
Integer ListSize = CustomerList.size();
return ListSize;
}
}

// Save the above class then execute as below
// Execute class using the thing of sophistication 
MyClassWithSharing obj = new MyClassWithSharing();
Integer ListSize = obj.executeQuery();

Without Sharing Keyword

As the name suggests, class declared with this keyword executes in System mode, i.e., regardless of the User’s access to the record, query will fetch all the records.

// Class Without Sharing
public without sharing class MyClassWithoutSharing {
List CustomerList = [SELECT id, Name FROM APEX_Customer__c LIMIT 10];

// Query To fetch 10 records, this may return all the records
public Integer executeQuery () {
System.debug('List will have only 5 records and therefore the actula records are'
+ CustomerList.size()+' as user has access to'+CustomerList);
Integer ListSize = CustomerList.size();
return ListSize;
}
}
// Output are going to be 10 records.

Setting Security for Apex Class

You can enable or disable an Apex class for particular profile. The steps for an equivalent are given below. you’ll determine which profile should have access to which class.
Setting Apex class security from the category list page
Step 1 − From Setup, click Develop → Apex Classes.

Salesforce
Salesforce

Step 2 − Click the name of the category that you simply want to limit . we’ve clicked on CustomerOperationClass.

Salesforce 1
Salesforce

Step 3 − Click on Security.

Salesforce 2
Salesforce

Step 4 − Select the profiles that you simply want to enable from the Available Profiles list and click on Add, or select the profiles that you simply want to disable from the Enabled Profiles list and click on on Remove.

Salesforce 3
Salesforce

Step 5 − Click on Save.

Setting Apex Security from Permission Set
Step 1 − From Setup, click Manage Users → Permission Sets.

Salesforce 4
Salesforce

Step 2 − Select a permission set.

Salesforce 5
Salesforce

Step 3− Click on Apex Class Access.

Salesforce 6
Salesforce

Step 4 − Click on Edit.

Salesforce 7
Salesforce

Step 5 − Select the Apex classes that you simply want to enable from the Available Apex Classes list and click on Add, or select the Apex classes that you simply want to disable from the Enabled Apex Classes list and click on remove.

Salesforce 8
Salesforce

Step 6 − Click the Save button.
So, this brings us to the end of blog. This Tecklearn ‘Apex Security’ blog helps you with commonly asked questions if you are looking out for a job in Salesforce. If you wish to learn Salesforce and build a career in Salesforce domain, then check out our interactive, Salesforce Certification Training: Admin 201 and App Builder, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Salesforce Certification Training: Admin 201 and App Builder

Salesforce Certification Training: Admin 201 and App Builder

About the Course

Salesforce Certification Training course will help you pass the Salesforce Administrator Exam (Admin 201) and the Salesforce App Builder (Dev 401) Exam. Concepts on Force.com Platform, AppExchange, SFDC Security Model, Service Cloud, Sales Cloud, Lightning App Builder, Salesforce Reports & Dashboard can be mastered in this Salesforce Training course. You can also configure the platform, manage users, find better ways to use the platform’s features, build applications with Salesforce Lightning, and more. Further, in this Salesforce certification training course, you will master App builder, Apex, Visualforce, etc.

Why Should you take Salesforce Admin 201 and App Builder Training?

• As per Indeed.com data, 200% global jump in Salesforce jobs since Jan 2016. Salesforce Certified Administrators earn an annual average salary of $87,000 but can go as high as $160,000 depending on their knowledge, skills, and experience.
• More than 200,000 companies worldwide use Salesforce platform. Salesforce leads the CRM market with 19.5 percent of market share – Forbes.
• The global CRM software market will reach US$40.26 billion in 2023, up from US$36.9 billion (2020) – Statista.

What you will Learn in this Course?

Salesforce Fundamentals

• Introduction to CRM concepts and Cloud computing
• Salesforce.com Overview and Fundamentals
• Understanding Salesforce Platform
Understanding Salesforce Platform
• Understanding Salesforce Terminologies and Introducing the force.com platform
• Understanding Salesforce Metadata and API
• Describe the capabilities of the core CRM objects in the Salesforce schema
• Identify common scenarios for extending an org using the AppExchange
• About Salesforce Certification
Introduction to Sales Cloud
• Sales Cloud
• Sales Process
• Sales Productivity Features
• Lead Management
• Lead auto response
• Lead assignment
• Web to lead
• Accounts and Contacts Management
• Opportunities
• Campaign Management
Security Model, User Management and Its Features
• Security Model Mind Map
• System Level or Org Level Security
• User Administration and Troubleshooting
• Permission Sets
• Profile Management
• User Actions
• Assigning Permission
• Session settings
• Activations
• Page layout assignment
• Tab setting
• Field level security
Object, Record and Field Level Features
• Custom Object
• Custom Field
• Data Types
• Relationship among Objects
• Working with App and Tabs
Data Handling and Processing
• Data Import and Export with Salesforce
• Insert, Update and Delete Data with Salesforce
• Export Data with UI
• Export Data using Data Loader Tool
Deployment
• SandBox
• Moving Data from SB to Production – Deployment
• Types of SandBox
• Change Sets
• Types of Change Sets
Application Cycle
• Milestones
• Sandboxes
• Change Sets
• Packages
Reports and Dashboards
Declarative Implementation in Salesforce
Salesforce Development and Apex Programming
• Apex Programming
• Apex Classes
• Apex Settings
• SOQL – Salesforce Object Query Language
• DML Commands
• Apex Class in Detail
• Apex Triggers
• Apex Testing
• Access Specifier in Salesforce
• Testing
Lightning in Salesforce
• Lightning Components
• Lightning Component Capabilities
• Lightning Components vs. Visualforce
Visual Force in Salesforce
• Standard Visualforce controller and controller extensions,
• Visualforce Page
• Understanding the MVC Pattern
• Tools for Visualforce Development
• Visual Force Components
WorkFlows in Salesforce
• Work Flows in Salesforce
• Types of Work Flows
• Work Flows Rules
About Preparation of Salesforce 201 and App Builder Certification exams

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Apex Security"

Leave a Message

Your email address will not be published. Required fields are marked *