trigger in Salesforce

Episode 5 – Demystifying Apex Triggers

Apex, Apex Hours for Students

In this episode we will learn about writing triggers to fire business logic using Apex. Join us as you embark on this wonderful journey to become a champion Salesforce developer.

Episode 5 will be presented by Purushottam Bhaigade on Feb 14, 2020 at 6 PM Indian Standard Time.

Agenda

  • Order of Execution
  • When & How Triggers are invoked
  • Trigger Events
  • Avoiding Recursive Trigger Calls
  • Q & A

Most importantly don’t break a leg if you are overwhelmed with the pace of the live sessions. All Apex Hours for Student sessions will be recorded and will be available on our YouTube channel.

Please register here and follow our ApexHours website to get more notification.

What is Trigger?

Trigger is a procedure in database which automatically invokes whenever a special events in Database occurs. Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions.

Apex Triggers

When Should we Use Trigger?

  • Complex logic incapable of being processed via declarative artifacts
  • Logic associated with DML
  • Operations on unrelated objects
  • Integration with External Systems

Trigger Events

Trigger Context Variable

Variable Usage
isExecuting It tells if code current context is trigger or not
isInsert It returns true if trigger fires on insert operation
isUpdate It returns true if trigger fires on update operation
isDelete It returns true if trigger fires on delete operation
isBefore It returns true if code is running in Before context
isAfter It returns true if code is running in After context
new It holds list of new versions of sObject records
This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers
newMap It holds new versions of sObject in key value pair
This map is only available in before update, after insert, after update, and after undelete triggers
old It holds list of old versions of sObject records
This sObject list is only available in update and delete triggers
oldMap It holds old versions of sObject in key value pair
This map is only available in update and delete triggers.
operationType Returns an enum of type System.TriggerOperation corresponding to the current operation. System.TriggerOperation enum are: BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE
size The total number of records in a trigger invocation, both old and new.

Demo 1 : Using context variable in Trigger

trigger AccountTrigger on Account (before insert, before update, after update) {
    if(Trigger.isBefore && Trigger.isInsert){
        System.debug('I am in AccountTrigger before insert context');
    }
    if(Trigger.isUpdate){
        if(Trigger.isBefore){
            for(Account acc :Trigger.new){
                System.debug('New Name'+ acc.Name);
                System.debug('Old Name'+ Trigger.oldMap.get(acc.Id).Name);
            }
        }
    }
}

Order of Execution.

Order of Execution
Order of Execution

Here is recording of our old session on Order of Execution. Check this blog post to learn more.

Trigger Handler pattern

Check this blog post to learn about which all trigger pattern are available in Salesforce. For Trigger Handler code check this post

Trigger Handler Pattern

Trigger Best Practice

1) One Trigger Per Object
2) Logic-less Triggers
3) Context-Specific Handler Methods
4) Bulkify your Code
5) Avoid SOQL Queries or DML statements inside FOR Loops
6) Using Collections, Streamlining Queries, and Efficient For Loops
7) Querying Large Data Sets
8) Use @future Appropriately
9) Avoid Hardcoding IDs

Always remember below points before writing trigger :-
1) Order Of Execution
2) Recursive Trigger
3) Learn about Other Trigger Framework

Recording

Assignment

Complete below assignment to win $1000 Salesforce Voucher. Click here for rule.

Create Custom object Invoice with field Total Amount
Create child object Invoice Line Item with Lookup relationship to Invoice.
Add price field into Invoice Line Item.
Write trigger on Invoice Line Item which will add some of all child records to parent Invoice

Trigger Handler Pattern

Further Learning

Trigger Framework

Don’t forget to register for our next session on Test Classes. Check this post for all other session detail.

Please note that we have limit of 500 attendees that can join the online sessions. However, recording will be posted on our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.

So, learn at your pace and free will and ace your journey to Salesforce!

5,554 total views, 36 views today

9 comments

Leave a Reply