Salesforce interview questions and answers : All About Triggers : Part 1

Que 1. What are types of triggers?

Ans 1. 

1.Before

 2.After

Que 2. What are events in triggers ? 

Ans 2. Following are events in writing triggers :

1. Before insert

2. After insert

3. Before update

4. After update

5. Before delete

6. After delete

7.After undelete

Que 3. What are context variables in triggers?

Ans 3. Following are context variables in triggers :

1.Trigger.isExecuting

2. Trigger.size()

3.Trigger.isBefore

4.Trigger.isInsert

5.Trigger.isAfter

6.Trigger.isUpdate

7.Trigger.isDelete

8.Trigger.isUndelete

9.Trigger.new

10. Trigger.newMap

11.Trigger.old

12.Trigger.oldMap

Que 4. Consider scenario 

I have two objects Parent__c and Child__c with parent-child relationship. OWD settings for Parent__c is Public Read/Write and for Child__c it is Private.

The User1 has Read/Write/Delete/Edit access on Parent__c and No Access on Child__c object.

I wrote a simple trigger on Parent__c on update to create a child record Child__c.

When I update the Parent record from User1, a new child record is created with the record owner as User1 ! And for obvious reasons, User1 gets insufficient privileges error when trying to access the child record that he owns.

Ans 4. 

Triggers run in system.mode so it has access equal to System.Admin and hence child records got created.
When you try to access record it becomes User1 context hence User does not have access to that object.
Also when there is master-detail relationship you can not provide OWD access individually to Parent and child. Child access is controlled by Parent. So there must be look up relationship.

Que 5. Is there any limit on number of triggers per object?

Ans 5. There is no limit for number of triggers defined per object.

But it is best practice to define single trigger per object as we can not guarantee which trigger will fire first and hence we can decide order of execution of code.

Que 6. What are best practices to follow while writing trigger?

Ans 6. 

1. Avoid DML statements and SOQL Queries in for loops to avoid hitting governor limits

2. One trigger per object as we can not guarantee which trigger will fire first and hence we can not  decide order of execution.

3.  Bulkify your trigger  i.e code should work if numbers or records inserted,updated ,deleted and undeleted in bulk.

4. Avoid Hard Coding IDs

5. Prevent recursive triggers.

Example : 

trigger  ContactTrigger on Contact(after insert)
{

if(trigger.isAfter && trigger.isInsert && !ContactTriggerHandler.isRecursive){

 ContactTriggerHandler.isRecursive=true;
 ContactTriggerHandler.isDupContacts(Trigger.New);
}
}

public class ContactTriggerHandler{

public static void isDupContacts(List<Contact> conList){

public static Boolean isRecursive=false;

List<Contact> dupContactList=new List<Contact>();

for(Contact con:conList)
{
Contact con1=new Contact();
con1.lastName='Duplicate Contact';
dupContactList.add(con1);
}
insert dupContactList;
}
}

6. Use comments to make your trigger readable

7. Do not write logics in trigger user Trigger Handler instead. (As shown in point no 5 example)

Que 7. Can a trigger Call a batch class?

Ans 7. Yes we can call batch class from trigger as we do in normal apex code.

Que 8.Can a trigger make a call to Apex callout method?

Ans 8. Yes ,we can call a callout method in Apex Trigger but the only condition is that it has to be an asynchronous callout because the trigger flow cannot wait on the response received by the callout method.

Comments

Popular posts from this blog

Salesforce interview questions and answers : All about SOQL : Part 1

All About Future methods