Below are the list of important Apex Interview Questions with Answers. You never know, you might come across these questions in your next interview.
Ques : Which SOQL statement can be used to fetch all records even from recycle bin ?
Answer: “ALL Rows” clause of SOQL can retrieve all records, including the records from recycle bin.
SELECT COUNT() FROM Contact WHERE AccountId = a.Id ALL ROWS
Question: How can you lock a record using SOQL to prevent it from being modified by another user?
Answer: We will need “FOR UPDATE” clause of SOQL.
Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];
Question: If you have set up more than one savepoint, and then you roll back to a savepoint which is not the last savepoint, what will happen to the later savepoint variables?
Answer: If you generated savepoint SP1 first and generated SP2 savepoint after that, and if you rolled back to SP1, the variable SP2 will no longer be valid. You will receive a runtime error if you try to use it.
Question: What are few limitations of Savepoint or Transaction control in Apex?
Answer: Below are some points to remember:
- Each savepoint you set counts against the Governor limit for DML statements.
- If you insert the ID on an SObject after setting the savepoint, the ID will not be cleared after a rollback.
- Static variables are not reverted during a rollback. If you run the trigger again, the static variables will retain the values from the previous run.
Question: What is the difference between public and Global class in Apex?
Public access modifier in Salesforce is different than the public modifier in Java. A public class can be accessed within an application or namespace. Whereas Global class is visible everywhere : in any application or namespace. Webservice must be declared as Global so that it can be accessed outside of the application. Global variable is similar to public modifier in Java.
Question: What are some points to remember regarding Static Keyword in apex.
- Apex classes cannot be static.
- Static variables are static only in scope of request.
- Static variables and static blocks runs in the same order as they are declared inside the class.
- Static variables are not transferred as a part of view state.