Triggers Introduction

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

Triggers:-

  • Apex can be in invoked through the use of triggers.
  • A Trigger are functional actions which gets fired on particular events.

That is Data can flow into Data.com at any time. Triggers will happen before records entering into the database and while going out of the database. Note:-        Each event is the firing Point. *onclick *Submit * A Trigger is Apex code that execute before or after the following types of operations.

  • Insert
  • Update *Delete
  • Undelete

We can have a trigger run before an Object's records are inserted into the database, after records have been deleted, or even after a record is restored from the Recycle BIN. * Triggers can be divided into two types.

  • Before triggers can be used to update or validate record values before they are saved to the DataBase.
  • After triggers can be used to access field values that are set by the database, and to affect changes in other records.

Events:-  Each event is the firing point events are formation of

Before Insert Before Update Before delete After Undelete
After insert After Update After Delete  

 

These core tutorials will help you to learn the fundamentals of Salesforce. For an in-depth understanding and gain 
practical experience,  explore Online Salesforce Training.

Note:- *Before insert Trigger are used mainly for validation rules. Before insert àData screening --->Data Insert *After insert triggers are used mainly for WorkFlow Rules.

Trigger Syntax:-

To define a Trigger, use the following syntax

Trigger TriggerName on ObjectName(trigger-events) {
Code Block;
}

Where trigger-events can be comma- separated list of one or more of the following events.

  • Before Insert
  • After insert
  • After Update
  • Before Update
  • Before delete
  • After Delete
  • After Undelete

EX:-  The following code defines a trigger for the before insert and before update events on the Account object.

Trigger Account _Trigger on Account (before insert, Before Update) {
------
-----
}

All triggers define implicit variables that allow developers to access run time context. These variables are contained in the system.Trigger class.

  • Trigger.New:- Trigger.new returns a list of the new records of the Sobjects which we are going to insert into the database.  This SObject list is only available in insert and Update triggers, and the records can only be modified in before triggers.
  • Trigger.old:- Trigger.old returns a list of old records of the Sobjects from the database. Note:- This Sobject list is only available in update and delete triggers.
  • Trigger.NewMap:- Trigger newMap returns a map of IDs to the new records of the Sobjects. Note:- This map is only available in before update, After insert, and after update triggers.
  • Trigger.OldMap:- Trigger.OldMap returns a map of ids to the old records of the SObjects.
  • Trigger.isAfter:- Trigger.isAfter context variable return true if this trigger was fired after all the records we saved.
  • Trigger.isBefore:- This context variable returns true if trigger was fired before any record was saved.
  • Trigger.is Insert:- This context variable returns true if the trigger was fired due to an insert operation.
  • Trigger .is Update:- This context variable returns true if the trigger was fired due to an update operation.
  • Trigger .is Delete: - This context variable returns true if trigger was fired due to a delete operation.
  • Trigger is Undelete:- This context variable returns true if trigger was fired ofte a record is recovered from the recycle bin

Note:-Trigger are object specific

 Context variable Consideration:-

  • Trigger.new and trigger.old cannot be used in apex DML operations.
  • We can use on object to change its own field values using trigger.new, but only in before triggers. In all after triggers, triggers.new is not saved so runtime exception is thrown.
  • Trigger.old is always read-only. · We can not delete trigger.new.
  • Before insert ==> Trigger.new allowed.
  • In after insert triggers ==> Trigger.new not allowed.
  • Trigger.new is not available in before delete triggers.
  • Trigger.old is not available after undelete triggers.

Bulk Triggers:-

All triggers are bulk triggers by default and can process multiple records at a time. We should always plan on processing, more than one record at a time. When we are uploading bulk records using the data loader, then also triggers will fire for all records at a time.

For indepth understandingng on Salesforce click on

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox