Posted in Trigger

My Best Friend Salesforce Trigger

Triggers

images              images (7)

Whenever I think about  “Trigger” in Salesforce .  First thing  is coming  in my mind is David(My real hero for Trigger). He is super genius person in trigger. Thanks @David for your website(Sdfc 99) and inspire me to do something special  in my life.

images (4)

Trigger is an apex code that gets executed whenever a specific DML operation is performed on an Object record. It allow developers to pro grammatically check for duplicate records, update fields across objects, automate the creation of new records based on criteria, and much more.

For example, if I wanted to check for duplicity on the user email field every time a user was created or updated I would write a trigger and assign the “before insert, before update” operations to it. This would allow me to query the database and throw an exception if a duplicate is found before saving to the database.

Triggers can be primarily classified into two types:

  • Before trigger
  • After trigger

Before Triggers:  Before triggers are ones that get executed before the record is saved to the database.

After Triggers: After triggers are ones that get executed after the record is saved to the database.

Each one has its own significance as each one has to be used in specific scenarios.

images (9)

Usage of Before Triggers:

  • Can be used to perform validations and stop the creation / updation / deletion of records if they are not satisfying the criteria.
  • Data can be modified in before triggers only, hence record field auto population by the system can be performed here.

Usage of After Triggers:

  • By the time these triggers execute the record ID and the system generated fields such as created by, modified by etc… are generated by the system and these fields such as ID can be used to build relationships i.e create relationship records.

Trigger Syntax:

trigger <triggerName> on <Object>( DML Events ) {

/* — Execute Trigger Logic Here — */

}

DML Events: Below are the DML events that can be specified on a trigger.

images (13)

  • Before Insert : Trigger that executes due to an insert operation and before the record is saved to the database it is a before Insert trigger
  • Before Update : Trigger that executes due to an update operation and before the record is saved to the database it is a before update trigger
  • Before Delete : Trigger that executes due to a delete operation and before the record is saved to the database it is a before delete trigger
  • After Insert :Trigger that executes due to an insert operation but after the record is saved to the database it is an after Insert trigger
  • After Update : Trigger that executes due to an update operation and after the record is saved to the database it is an after update trigger
  • After Delete : Trigger that executes due to a delete operation and after the record is deleted from the database it is an after delete trigger
  • After Undelete : Trigger that executes due to a undelete operation and after the record is undeleted from the database it is an after undelete trigger

images (12)              images (8)

Trigger Context Variables:

Variable Usage
isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an execute anonymous() API call.
isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBefore Returns true if this trigger was fired before any record was saved.
isAfter Returns true if this trigger was fired after all records were saved.
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 theAPI.)
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.

newMap A map of IDs to the new versions of the sObject records.

Note that this map is only available in before update, after insert, and after update triggers.

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.

oldMap A map of IDs to the old versions of the sObject records.

Note that this map is only available in update and delete triggers.

Size The total number of records in a trigger invocation, both old and new.

Context Variable Considerations:   Be aware of the following considerations for trigger context variables:

  • trigger.new and trigger.old cannot be used in Apex DML operations.
  • You can use an object to change its own field values using trigger.new, but only in before triggers. In all after triggers, trigger.new is not saved, so a runtime exception is thrown.
  • trigger.old is always read-only.
  • You cannot delete trigger.new.

images (6)

The following table lists considerations about certain actions in different trigger events:

Trigger Event Can change fields using trigger.new Can update original object using an update DML operation Can delete original object using a delete DML operation
beforeinsert Allowed. Not applicable. The original object has not been created; nothing can reference it, so nothing can update it. Not applicable. The original object has not been created; nothing can reference it, so nothing can update it.
afterinsert Not allowed. A runtime error is thrown, as trigger.newis already saved. Allowed. Allowed, but unnecessary. The object is deleted immediately after being inserted.
beforeupdate Allowed. Not allowed. A runtime error is thrown. Not allowed. A runtime error is thrown.
afterupdate Not allowed. A runtime error is thrown, as trigger.newis already saved. Allowed. Even though bad code could cause an infinite recursion doing this incorrectly, the error would be found by the governor limits. Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible.
beforedelete Not allowed. A runtime error is thrown. trigger.new is not available in before delete triggers. Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible. Not allowed. A runtime error is thrown. The deletion is already in progress.
afterdelete Not allowed. A runtime error is thrown. trigger.new is not available in after delete triggers. Not applicable. The object has already been deleted. Not applicable. The object has already been deleted.
afterundelete Not allowed. A runtime error is thrown. trigger.old is not available in after undelete triggers. Allowed. Allowed, but unnecessary. The object is deleted immediately after being inserted.

Order of execution of triggers in Salesforce environment:

images (11)                 images (1)

  • Old record loaded from database (or initialized for new inserts)
  • New record values overwrite old values
  • System Validation Rules
  • All Apex “before” triggers
  • User Validation Rules
  • Record saved to database (but not committed)
  • Record ID and System generated fields are generated.
  • Record reloaded from database.
  • All Apex “after” triggers
  • Assignment rules
  • Auto-response rules
  • Workflow rules
  • Escalation rules
  • Parent Rollup Summary Formula value updated (if present)
  • Database commit
  • Post-commit logic (sending email)

images (3)                      images (5)

Advertisements

Author:

I am 3X Salesforce Certified. Most important thing which I like about Salesforce is giving back. There aren’t enough words in the dictionary to describe the significance of giving back.

2 thoughts on “My Best Friend Salesforce Trigger

  1. WOW! So glad I could help inspire you Pritam!

    Give yourself a ton of credit for finding success in your life. It’s one thing to get inspired, and it’s a whole other thing to actually execute from there. I have a ton of respect for you and what you’ve done so far… very impressive!

    I hope there’s a lot more to come too =)

    David

    Liked by 1 person

    1. Yes @David , there’s a lot more to come too. Now it’s start of my journey. I want to do something more special like you. You inspired me a lot. Thanks for your inspiration. From your every post i will learn something different. And thanks again for being with me in my whole journey in Salesfoce 🙂

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s