Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home1/oijoiv2f/public_html/wp-content/themes/entaro/template-posts/single/inner.php on line 23

Salesforce Interview Questions on Trigger

Salesforce Interview Questions on Trigger

What is a Trigger?

Trigger can invoke Apex code, they come in handy when we want to perform some custom task just before or after a record is either created updated or deleted.

What are the various event on which a trigger can fire?

A trigger is a set of statement which can be executed on the following events:

  1. Undelete
  2. Update
  3. Merge
  4. Delete
  5. Upsert
  6. Insert

 Broadly classify the Trigger?

Triggers can be broadly classified as before or after Trigger.

  • Before triggers are used to perform a task before a record is inserted or updated or deleted.
  • After triggers are used if we want to use the information set by Salesforce system and to make changes in the other records

What are the considerations while implementing the Triggers?

Consider the following before implementing the triggers.

  • Upsert trigger fires on 4 different events :- before(insert, update), after (insert, update)
  • Merge trigger are fired on both events on delete
  • Field history is updated after the trigger has successfully finished processing data.
  • Any callout should be asynchronous so that trigger does not have to wait for the response.
  • A trigger cannot have a static keyword in its code.
  • If a trigger completes successfully the changes are committed to the database and if it fails the transaction is rolled back.

Read the Apex Developer Guide for more detailed considerations.

Write the syntax of Apex Trigger?

Trigger TName On ObjName(name the events){

……. Apex code here ……..

}

What are context variables in regards to trigger?

Following are the context variable available in triggers. Please note variable’s availability varies according to the type of trigger events.

  1. isExecuting
  2. isInsert
  3. isUpdate
  4. isDelete
  5. isBefore
  6. isAfter
  7. isUndelete
  8. new
  9. newMap
  10. old (update and delete only)
  11. oldMap (update and delete only)
  12. size

How is Trigger.New Different from Trigger.newMap?

Trigger.New variable returns the list of sObject which has invoked the trigger and Trigger.NewMap returns the map of ID’s with the newly entered records. NewMap is only available in after insert, before and after the update and after undelete.

How is Trigger.new different from Trigger.old?

Trigger.New variable returns the list of sObject which has invoked the trigger and Trigger.old returns a list of the older versions of the records which have invoked the trigger. Trigger.Old is only available in update and delete events

Can a trigger call a batch class?

Yes, we can call a batch class in the trigger as we do in the normal apex code.

Can a trigger make a call to Apex callout method?

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.

Define Recursive Trigger and how to avoid it?

There is a possibility that the result of the trigger can end up calling the same trigger again and can run in a loop, this is known as a recursive trigger. To avoid this scenario we should create a static variable and check the value of this variable before we execute anything in the trigger.

What do you mean by the bulkifying trigger?

A trigger that can handle both single record and thousands of record. It is capable of handling mass action.

Is there any limit on number of triggers define on an object?

We can define as many triggers on an object as we want but it is recommended to have one trigger per object because the order of execution of different trigger is not guaranteed and any trigger can fire first.

Can you explain the order of execution in Triggers?

Following is the order of execution of events which Salesforce perform before a DML Event.

  1. The record is loaded from the database or is initialized in case of upset statement.
  2. New record’s field values are overwriting the old values, now depending on the origin of the request this flow varies: if the request is from a UI page then the following validations are performed by Salesforce:
    1. Any layout specific rules are checked
    2. All the required values are checked at layout and field level
    3. All the field formats are validated along with the maximum length of field values

If the request originates other than UI then Salesforce only checks for Validation of foreign keys.

  1. Now all the before triggers are executed at the database.
  2. Most of the validations are performed again to verify that all the required fields are holding some values and are not null, at this step user defined validations are also executed and the only validation which is not repeated in this step are the rules specific to the layout.
  3. After the success of the previous step, the record is reviewed for duplicate records, by running the duplicate rule. If a duplicate is found the flow is stopped and no further actions performed.
  4. In this step, record is saved to the database but it not committed yet.
  5. Now all the after Triggers are executed.
  6. In this step, assignment rules are executed.
  7. Now if there is any auto-response rule is present then they are executed.
  8. Next in the queues are the workflow, they are executed after the auto response.
  9. If the workflow was updating a field, then the fields updated in this step and the flow after this step varies if this was the case.
  10. If a field was updated then the before and after update triggers are fired once more and standard validation are also executed again. Custom validation escalation rule and duplicate rules are not required to run again.
  11. Once the execution has reached this stage, then process is fired if there are any declared on the object.
  12. Now the escalation rules are executed.
  13. Entitlement rules are executed if any.
  14. If there are any roll-up summary field, then they are calculated at this step and the parent object go through the save process.
  15. Now the sharing rules are executed.
  16. If we reach this stage, then that means no error has occurred and the data is ready to be committed to the database and is committed now.
  17. Now if there is any post-commit logic like email, then that is executed.
Sumit Datta

Sumit Datta

I am a 5x Certified Salesforce developer with overall 7 years of IT experience and 5 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

11 Comments

  1. bhavya July 13, 2017 Reply

    Thank you so much sumit for sharing these que-ans.

  2. […] based Apex Collections Q.What are the types of collections which are supported by salesforce? A. Salesforce Interview Questions on Trigger | SalesforceNextGen. Salesforce Interview Questions on Trigger What is a Trigger? Trigger can invoke Apex code, they […]

  3. sushe kumar July 13, 2017 Reply

    Thanks you so much sumit.

  4. […] Interview Questions Apex Trigger […]

  5. […] Interview Questions Apex Trigger […]

  6. Balaji July 13, 2017 Reply

    Hi Sumit,

    One doubt in trigger. Trigger.New has list of new versions of records and Trigger.newMap.values() also has list of new versions of records.So why we need to use Trigger.New. Why Trigger.New specifically used by salesforce.

    • Sumit Datta
      Sumit Datta July 13, 2017 Reply

      Hi Balaji

      Trigger.newMap dont work for before insert while Trigger.New works fine for holding all Ids of records while inserting.

      According to the docs, Trigger.new returns a list, which are ordered, and Trigger.newMap returns a map – which are unordered. The docs specifically state you should not rely on the ordering of a map’s elements.

      What to use depends on what you’re doing – if you have an ID of an object you need to do something with, using the map makes more sense as you can use newMap.get(). Otherwise you’d have to loop over all the elements in Trigger.new and look for a matching ID. Similarly, if you have multiple loops over each item the trigger is operating on, the list returned by Trigger.new may be the better bet.

  7. Vaishali July 13, 2017 Reply

    Nice set of questions.

    Thank you.

  8. Vaishali July 13, 2017 Reply

    I have one question : Do we get id of record in trigger.new ? I am sure about trigger.newMap() but not about trigger.new().

    Thank you.

    • Pankaj Saini July 13, 2017 Reply

      yes,we get id in trigger.new if the trigger is fired after insert

    • Pankaj Saini July 13, 2017 Reply

      yes, we get Id in trigger.new if the trigger is fired after insert operation..

Leave a Comment

Your email address will not be published.