1. Salesforce Interview Questions on Trigger
Q. What is trigger?
A. Triggercan invoke apex code,theycome inhandywhenwe wanttoperformsome customtaskjust
before orafterrecord iseithercreatedupdatedordeleted.
Q. What are the various eventon which a trigger can fire?
A. Triggerisa setof statementwhichcanbe executedonthe followingevents:
1. Undelete
2. Update
3. Merge
4. Delete
5. Upsert
6. Insert
Q. Broadly classifythe Trigger?
A. Triggerscan be broadlyclassifiedasbefore orafterTrigger.
Before triggersare usedtoperforma task before arecord isinsertedorupdatedordeleted.
Aftertriggersare usedif we want to use the informationsetbySalesforce systemandtomake
changesinthe otherrecords
Q. What are the considerationswhile implementingthe Triggers?
A. Considerthe followingbeforeimplementingthe triggers.
Upsert triggerfireson4 differentevents:- before(insert,update),after(insert,update)
Merge triggerare firedonboth eventsondelete
Fieldhistoryisupdated afterthe triggerhassuccessfullyfinishedprocessingdata.
Anycalloutshouldbe asynchronoussothattriggerdoesnot have to waitforthe response.
Triggercannot have a static keywordinitscode.
If a triggercompletessuccessfullythe changesare committedtothe database andif it failsthe
transactionisrolledback.
Readthe Apex Developerguide formore detailedconsiderations.
Q. Write the syntax of ApexTrigger?
A. TriggerTName OnObjName(namethe events){
……. Apex code here ……..
}
Q. What are context variablesin regards to trigger?
A. Followingare the contextvariable available intriggers.Pleasenote variable’savailabilityvaries
accordingto the type of triggerevents.
1. isExecuting
2. isInsert
3. isUpdate
2. 4. isDelete
5. isBefore
6. isAfter
7. isUndelete
8. new
9. newMap
10. old(update anddelete only)
11. oldMap(update anddelete only)
12. size
Q. How isTrigger.NewDifferentfrom Trigger.newMap?
A. Trigger.New variable returnsthe listof sObjectwhichhasinvokedthe triggerandTrigger.NewMap
returnsthe map of ID’s withthe newlyenteredrecords.NewMapisonlyavailable inafterinsert,before
and afterupdate andafter undelete.
Q. How isTrigger.new differentfromTrigger.old?
A. Trigger.New variable returnsthe listof sObjectwhichhasinvokedthe triggerandTrigger.old returns a
list of the older versions of the records which has invoked the trigger. Trigger.Old is only available in update and
delete events
Q. Can a trigger call a batch class?
A. Yeswe can call a batch classin triggeras we do inthe normal apex code.
Q. Can a trigger make a call to apexcallout method?
A. we can call a calloutmethodinApex Triggerbutthe onlyconditionisthatithasto be an
asynchronouscallout,because the triggerflow cannotwaitonthe response receivedbythe callout
method.
Q. Define Recursive Trigger and how to avoid it?
A. There isa possibilitythatthe resultof triggercanend upcallingthe same triggeragainand can run in
a loop,thisisknownas a recursive trigger.Toavoidthisscenariowe shouldcreate astatic variable and
checkthe value of thisvariable beforewe execute anythinginthe trigger.
Q. What do you mean by bulkifyingtrigger?
A. A triggerthat can handle bothsingle recordandthousandsof record.It iscapable of handlingmass
action.
Q. Is there any limiton number of triggerdefine on a object?
A. We can define asmanytriggeron an objectas we wantbut it isrecommendedtohave one triggerper
objectbecause the orderof executionof differenttriggerisnotguaranteedandanytriggercan fire first.
3. Q. Can you explainthe order of executioninTriggers?
A. Followingisthe orderof executionof eventswhichsalesforce performbefore aDML Event.
1. Recordis loadedfromdatabase oris initializedincase of upsetstatement.
2. Newrecordfiledvaluesare overwritingthe oldvalues,now dependingonthe originof the
requestthisflowvaries:if the requestisfroma UI page thenthe followingvalidationsare
performedbysalesforce:
a. Anylayoutspecificrulesare checked
b. All the requiredvaluesare checkedatlayoutandfieldlevel
c. All the fieldformatsare validatedalongwiththe maximumlengthof fieldvalues
If the requestoriginatesotherthanUI thenSalesforce onlychecksforValidationof foreignkeys.
3. Nowall the before triggersare executedatthe database.
4. Most of the validationsare performedagaintoverifythatall the requiredfieldsare holding
some valuesandare not null,at thisstepuserdefinedvalidationsare alsoexecutedandthe only
validationwhichare notrepeatedinthisstepare the rulesspecifictolayout.
5. Afterthe successof previousstep,the recordisreviewedforduplicate records,byrunningthe
duplicate rule.If aduplicate isfoundthe flow isstoppedandnofurtheractionsperformed.
6. In thissteprecordis savedtodatabase but itnot committedyet.
7. Nowall the afterTriggersare executed.
8. In thisstepassignmentrulesare executed.
9. Nowif there isany auto response rule ispresentthentheyare executed.
10. Nextinthe queuesare the workflow,theyare executedafterthe autoresponse .
11. If the workflowwasupdatingafield,thenthe fieldsupdatedinthisstepandthe flow afterthis
stepvariesif thiswasthe case.
12. If a fieldwasupdatedthenthe before andafterupdate triggersare firedonce more and
standardvalidationare alsoexecutedagain.Customvalidationescalationrule andduplicate
rulesare not requiredtorunagain.
13. Once the executionhasreachedthisstage,thenprocessare firedif there are anydeclaredon
the object.
14. Nowthe escalationrulesare executed.
15. Entitlementrulesare executedif any.
16. If there are any roll upsummaryfield,thentheyare calculated atthisstepandthe parentobject
go throughthe save process.
17. Nowthe sharingrulesare executed.
18. If we reachthisstage,thenthat meansno errorhas occurred andthe data isreadyto be
committedtothe database and iscommittednow.
19. Nowif there isany postcommitlogiclike email,thenthatisexecuted.