Development
Salesforce Triggers Interview Questions and Answers Experienced
What is Trigger in Salesforce?
Ans: The trigger is piece of code that executes before or after event on the following operations such as insert, update, delete, undelete etc. trigger enables to perform custom actions before and after modifications to the records of Salesforce.
2. What are different Salesforce Editions are available.
Ans:
trigger trigger_name on SobjectName (event operation1, event operation 2){
}
Example:
trigger prefixName on Account (before Insert, before Update){
}
3. What are different event of trigger
Ans:
before event
after event
4. What are different operations in trigger
Ans: Insert, update, delete, undelete etc.
5. What is context variables and which are they?
Ans:
Context variable is used to bifurcate different operation logics.
OR
All triggers define implicit variables that allow developers to access run-time context. These variables are contained in the System.Trigger class.
Here is List of all Trigger Context Variables
• Trigger.isExecuting: Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
• Trigger.isInsert: Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
• Trigger.isUpdate: Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
• Trigger.isDelete: Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
• Trigger.isBefore: Returns true if this trigger was fired before any record was saved.
• Trigger.isAfter: Returns true if this trigger was fired after all records were saved.
• Trigger.isUndelete: Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
• Trigger.new: Returns a list of the new versions of the sObject records. This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.
• Trigger.newMap: A map of IDs to the new versions of the sObject records. This map is only available in before update, after insert, after update, and after undelete triggers.
• Trigger.old : Returns a list of the old versions of the sObject records. This sObject list is only available in update and delete triggers.
• Trigger.oldMap: A map of IDs to the old versions of the sObject records. This map is only available in update and delete triggers.
• Trigger.size: The total number of records in a trigger invocation, both old and new.
6. Difference between trigger.new and trigger.old
Ans:
Triger.new is a command which returns the list of records that have been added recently to the sObjects. To be more precise, those records will be returned which are yet to be saved to the database. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
But, Trigger.old returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.
7. Difference between trigger.new and trigger.newMap
Ans:
Trigger.new
Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
Trigger.NewMap
A map of IDs to the new versions of the sObject records, this map is only available in before update, after insert, and after update triggers.
suppose you have a Account object,
Trigger.new means it is a List
And,
Trigger.newMap means it is a Map
NOTE : In before insert context your Trigger.NewMap will always be null because in before context records are not submitted to the database, so the Id is not generated yet. That's why in before insert we don't use Trigger.NewMap But in After insert, Id is generated so we can use Trigger.NewMap
In case of before and after update, the Id has already been generated in the insert event. So we can use Trigger.NewMap in before and after update.
8. Difference between tigger.old and trigger.oldMap
Ans:
Trigger.old: Returns a list of the old versions of the sObject records.
Note that this sObject list is only available in the update and delete triggers.
Trigger.oldMap: A map of IDs to the old versions of the sObject records.Note that this map is only available in the update and delete triggers.
suppose you have a Account object,
Trigger.old means it is a List
And,
Trigger.oldMap means it is a Map
9. Difference between trigger.newMap and trigger.oldMap
Ans:
Trigger.NewMap:
It returns map of new records which are trying to insert into Database. This is available in Before Insert, Before Update, After Insert, After Update Triggers and undelete Triggers. This list of records can only modified in Before triggers.
Trigger.OldMap:
It returns map of old records which are updated with new values. These List of records already there in Database. Trigger.oldmap available in Before update, after update, Before Delete and After Delete triggers.
10. What is handler and helper in trigger
Ans:
To make a trigger logic less, we use handler and helper apex class. This also helps to better code readability and code re-usability.
11. Best practice of triggers
Ans:
1) One Trigger Per Object
A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts.
2) Logic-less Triggers
If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org.
3) Context-Specific Handler Methods
Create context-specific handler methods in Trigger handlers.
4) Bulkify your Code
Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.
5) Avoid SOQL Queries or DML statements inside FOR Loops
An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception
6) Using Collections, Streamlining Queries, and Efficient For Loops
It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits
7) Querying Large Data Sets
The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore.
8) Use @future Appropriately
It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found.
9) Avoid Hardcoding IDs
When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail.
12. What is recursive trigger ? How to avoid it.
Ans:
Recursion occurs when same code is executed again and again. It can lead to infinite loop and which can result to governor limit sometime. Sometime it can also result in unexpected output.
OR
When a trigger calls to itself again and again, then this concept is known as trigger (In this case: trigger calls itself at 16 Times and then throw recursion exception).
This will give you recusrsive error
trigger AccountTrigger on Account (before Insert) {
if(trigger.isInsert){
AccountTriggerHandler.insertNewAcc(trigger.new);
}
}
public class AccountTriggerHandler {
To avoid recursion, use a static Map.
public static void insertNewAcc(List
for(Account objAcc : accNewList){
Account objAcc1 = new Account(Name = objAcc.Name);
insert objAcc1;
}
}
}
public class AccountTriggerHandler {
public static Map
public static void insertNewAcc(List
List
for(Account objAcc : accNewList){
if(!accMap.containsKey(objAcc.Name)){
accMap.put(objAcc.Name, accMap);
Account objAcc1 = new Account(Name = objAcc.Name);
accListInsert.add(objAcc1);
}
}
if(!accListInsert.isEmpty())
Database.insert(accListInsert, false);
}
}
13. How many times a trigger will run for 1000 records
Ans:
Trigger runs in the batches of 200. That means 200 records per trigger.
(Number of Records)/200 = Total Trigger runs
1000/200 = 5 Times.
14. Which Trigger framework you people used in your project.
Ans:
Trigger Dispatcher Framework.
In this we can active or inactivate trigger in the run time. Also, it gives us a central command over triggers.