Salesforce Probable Interview Questions Part 2 - Apex (2022)
1. Apex :
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Lightning platform server in conjunction with calls to the Lightning Platform API.
reference -
https://help.salesforce.com/s/articleView?id=sf.code_about.htm&type=5 https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_what_is_apex.htm
2. Can we change Apex code in Production directly ?
No. We have to change and deploy through a sandbox and make sure that we meet test coverage requirements.
3. Before and After Trigger -
Before operation is used to verify information that is going to be inserted. This are used to update or validate record values before they’re saved to the database.
After trigger is used to access data that has previously been entered by a user or system. These are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records. The records that fire the after trigger are read-only.
reference - https://trailhead.salesforce.com/content/learn/modules/apex_triggers/apex_triggers_intro#:~:text=Before%20triggers%20are%20used%20to,after%20trigger%20are%20read%2Donly.
4. Governor Limits -
Because Apex runs in a multitenant environment, the Apex runtime engine strictly enforces limits so that runaway Apex code or processes don't monopolize shared resources.
reference - https://developer.salesforce.com/docs/atlas.en-us.236.0.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm
5. Apex Test Coverage -
Make sure that the written code meets certain standards, it shows the executable lines of code in your classes and triggers that have been exercised by test methods.
The percentage is a calculation of the number of covered lines divided by the sum of the number of covered lines and uncovered lines.
The minimum test coverage required so that we can deploy to production successfully is 75% and we have to make sure that we dont have any test failure.
Additionally, we have to follow the test class best practices.
reference -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_code_coverage_intro.htm
https://help.salesforce.com/s/articleView?id=sf.code_dev_console_tests_coverage.htm&type=5
6. Examples of Apex Best practices -
Bulkify Apex Code
Avoid SOQL & DML inside for Loop
Use of the Limits Apex Methods to Avoid Hitting Governor Limits
Use a Single Trigger per SObject Type
Modularize Your Code
Use Database Methods while doing DML operation
Exception Handling in Apex Code
Use Asynchronous Apex
Make reusability of Apex Code
Code coverage
Avoid nesting loops within loops
Naming Conventions
Peer Code Review and many more.
reference -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm
https://trailhead.salesforce.com/content/learn/modules/success-cloud-coding-conventions/improve-your-apex-code-sc
7. Collections in Apex -
Lists - This is an ordered collection of elements that are distinguished by their indices. List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Sets - This is an unordered collection of elements that do not contain any duplicates. Set elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Maps - This is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Reference -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections.htm
https://trailhead.salesforce.com/content/learn/modules/apex-basics-for-admins/use-collections
8. Static variable in Apex -
This is static only within the scope of the Apex transaction.
It's not static across the server or the entire organization.
The value of a static variable persists within the context of a single transaction and is reset across transaction boundaries.
reference -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm#:~:text=A%20static%20variable%20is%20static,is%20reset%20across%20transaction%20boundaries.
9. apex:actionPoller,apex:actionFunction, and other Action Methods in Salesforce -
apex:actionPoller-
This is a timer that sends an AJAX request to the server according to a time interval that you specify.
Each request can result in a full or partial page update.
An <apex:actionPoller> must be within the region it acts upon.
For example, to use an <apex:actionPoller> with an <apex:actionRegion>, the <apex:actionPoller> must be within the <apex:actionRegion>.
apex:actionFunction-
This provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component.
Binding between the caller and <apex:actionFunction> is done based on parameter order so ensure that the order of <apex:param> is matched by the caller's argument list.
Action Methods-
This performs logic or navigation when a page event occurs, such as when a user clicks a button, or hovers over an area of the page.
Action methods can be called from page markup by using {! } notation in the action parameter of one of the following tags:
<apex:commandButton> - This creates a button that calls an action
<apex:commandLink> - This creates a link that calls an action
<apex:actionPoller> - This periodically calls an action
<apex:actionSupport> - This makes an event (such as “onclick”, “onmouseover”, and so on) on another, named component, call an action
<apex:actionFunction> - This defines a new JavaScript function that calls an action
<apex:page> - This calls an action when the page is loaded
More - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_controller_setter_methods.htm
reference -
https://developer.salesforce.com/docs/atlas.en-us.234.0.pages.meta/pages/pages_compref_actionPoller.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm#:~:text=A%20component%20that%20provides%20support,component.
10. apex:actionRegion in Salesforce
This is an area of a Visualforce page that demarcates which components should be processed by the Force.com server when an AJAX request is generated.
reference - https://developer.salesforce.com/docs/atlas.en-us.234.0.pages.meta/pages/pages_compref_actionRegion.htm#:~:text=An%20area%20of%20a%20Visualforce,the%20performance%20of%20the%20page.
11. Transaction Control (Savepoint, Rollback) in Apex -
Savepoint returns a savepoint variable that can be stored as a local variable which we can use with a rollback method to restore the database to that point.
This a point in the request that specifies the state of the database at that time. Any DML statement that occurs after the savepoint can be discarded, and the database can be restored to the same condition it was in at the time you generated the savepoint.
Rollback restores the database to the state specified by the savepoint argument. Static variables are not reverted during a rollback. If you try to run the trigger again, the static variables retain the values from the first run
reference - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_transaction_control.htm
12. Apex Scheduler -
To invoke Apex classes to run at specific times, first we have to implement the Schedulable interface for the class, then specify the schedule using either the Schedule Apex page in the Salesforce user interface, or the System.schedule method.
reference -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex/async_apex_scheduled
13. Batch Apex in Salesforce -
This allows asynchronous processing of the records in multiple batches.
This implements the Database.batchable interface.
The interface has Start(), Execute() and Finish() methods which should be used in the Batch Apex class.
Start method is a collection point for the records to be processed in the execute method of the batch apex
Execute method is where actual processing of the records takes place in the execute method.
Finish method can have post job processing actions like sending the emails or it can also be used to chain another batch class.
reference -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex/async_apex_batch
14. apex:pageMessages in Apex -
This is used to displays all messages that were generated for all components on the current page, presented using the Salesforce styling.
reference -
https://developer.salesforce.com/docs/atlas.en-us.234.0.pages.meta/pages/pages_compref_pageMessages.htm#:~:text=This%20component%20displays%20all%20messages,a%20field%20on%20an%20sObject.
15. Flow vs. Apex in Salesforce -
Apex Code requires a developer and Sandbox to deploy. Flows can be built in all editions, as a Sandbox is not required for deployment.
Apex code requires constant development, making sure we follow all best practices and optimize. Flows requires bit less work than Apex if we follow all best practices from the starting. Though both of them requires us to follow the best practices and optimization.
16. Controller Methods - Action Methods, Getter Methods, Setter Methods
Action Methods - This perform logic or navigation when a page event occurs, such as when a user clicks a button, or hovers over an area of the page.
Getter Methods - This return values from a controller. Every value that is calculated by a controller and displayed in a page must have a corresponding getter method.
Setter Methods - This pass user-specified values from page markup to a controller. Any setter methods in a controller are automatically executed before any action methods.
reference-
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_methods.htm
17. Custom Controllers and Controller Extensions-
Custom Controller is an Apex class that implements all of the logic for a page without using a standard controller. We should use custom controllers when we want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.
Controller Extension is an Apex class that extends the functionality of a standard or custom controller like when we want to add new actions etc..
reference - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_def.htm
18. Standard Controller, Custom Controller, Extension -
Standard Controller-
-This inherits all the standard object properties and standard button functionality etc. directly.
-This contain the same functionality and logic that are used for standard Salesforce pages etc..
-This can be used with standard objects and custom objects.
-This can be extended to implement custom functionality using 'extensions' keyword.
-We will be getting access to the following methods and we can use them to perform some standard out-of-the-box functionalities.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardController_methods.htm
Custom Controller-
This is an Apex class that implements all of the logic for a page without using a standard controller.
These are associated with Visualforce pages through the controller attribute.
This defines its own functionality.
e.g. When we want our Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.
If we want to override the methods with our own custom functionality or implement a new method that not part of the mentioned methods(https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardController_methods.htm) then we need to create a Custom Controller.
Extensions-
An Apex Custom Controller and Apex Extension are both one but the difference is that in an Extension we will be having a compulsory argument constructor which is not the case with a Custom Controller.
reference-
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_additional_controller.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller.htm
Comments
Post a Comment