Salesforce Developer interview Questions – Apex Continued
Table of Contents
16. Why do we use transaction Control statements?
We use transaction control statements in a scenario where we are executing multiple dml statements and we want to roll back all the DML’s if one dml fails.
17. What are transaction Control statements?
are transaction control statements .
These are available in the Database Class.
18. Can you write sample code for a Transaction Control statement?
19. What is mixed DML operations error?
When we perform DML operations of setup object and non setup object in a transaction then we get mixed dml operation error.
Inserting an account object and user with role record in a same transaction
20. What are setup and non setup objects?
- UserRole etc
Non Setup Objects:
- Invoice__C etc
21. Can you tell me about a few objects which do not support DML operations?
There are few standard object which are also known as system objects which does not support DML operations like
- Territory2 etc
22. What is a record locking or locking statement in salesforce?
In Apex, we can use FOR UPDATE to lock sObject records while they’re being updated in order to prevent race conditions and other thread safety problems.
When an sObject record is locked, no other client or user is allowed to make updates either through code or salesforce user interface.
23. What is the benefit of a Locking statement?
When we use a locking statement then we can perform logic on the records and make updates which guarantee that the locked records won’t be changed by another client during the lock period.
The lock gets released when the transaction completes.
24. What are deadlocks?
Apex has the possibility of deadlocks as any other procedural programming language involving updates to multiple database tables or rows.
A deadlock is a situation in which two or more transactions are waiting for one another to give up locks.
25. How to avoid deadlocks?
To avoid deadlocks , the apex runtime engine
- First locks sObject parent records, then children.
- Locks sObject records in order of ID when multiple records of the same type are being edited.
As a developer we should make sure we are not introducing deadlocks. We should verify using standard deadlock avoidance techniques by accessing tables and rows in the same order from all locations in an application
26. What is dynamic Apex?
Dynamic apex enables us to create more flexible applications using describe information. By using dynamic apex, we can write generic code that can be reused repeatedly with multiple sObjects.
Example : With dynamic objects we don’t have to explicitly declare the name of the object like account , lead or opportunities. Based on the condition we can dynamically use any of the object and its fields
Example : We can also retrieve list of all the objects and its fields using dynamic apex etc
27. What is dynamic SOQL?
Dynamic SOQL refers to the creation of a SOQL string at runtime with Apex code.
Dynamic SOQL enables us to create more flexible applications.
Example : we can dynamically create a query based on multiple if else conditions.
Use Database query method to create dynamic SOQL
28. What is dynamic SOSL?
Dynamic SOSL refers to the creation of a SOSL string at runtime with apex code.
29. What is “With Sharing” and “Without Sharing” in apex?
“With Sharing” and “Without Sharing” keywords are used to ENFORCE or NOT ENFORCE users’ permissions and field-level security .
Generally, apex runs in system administrator context , that means the current user’s permissions and field-level security is not enforced. To enforce current users’ permission, we use “with sharing” keyword
30. What if we do not use “With Sharing” and “Without Sharing”?
By default, apex class runs “without sharing” i.e., in system admin context.
31. If apex runs “without sharing” then why do we have the keyword “without sharing”?
This is mostly used for inner class. Or in a situation we are calling methods from multiple classes Example 1
|Class A||With sharing|
|Class B||No sharing key word used|
|Class A calls Method in Class B||Both Class A and Class B run as “with Sharing” context because parent class or calling class is “with sharing”|
|Class A||With sharing|
|Class B||Without Sharing|
|Class A calls Method in Class B||Class A runs “with sharing” while class B runs “without sharing” context because we have explicitly mentioned the without sharing keyword.|
32. What is the impact of enforcing sharing rules i.e. using “with sharing” keyword on apex class?
SOQL and SOSL Queries : A query may return fewer rows than it would return in system context.
DML Operations : An operation may fail because the current user doesn’t have the correct permissions.
33. What is Apex Sharing?
Normally we share record using the following configurations
- Record ownership
- Role Hierarchy
- Sharing rules
- Manual Sharing
But Apex sharing is nothing but sharing the records programmatically. When all the above config options to share the record does not work for our scenario then we use apex sharing
34. Can you elaborate how we can share records using apex sharing or programmatically?
Salesforce has provided a “Share” object for every object . All the sharing details of that object’s records can be stored in share object
- Account object have AccountShare Object
- Lead object have LeadShare Object
- Opportunity object have Opportunity Share object
- Invoice__c will have invoice_share object