How to Play with Apex Triggers
Definition
of Apex Triggers:--
A trigger is an Apex code that
executes before or after the following types of operations:
- insert
- update
- delete
- merge
- upsert (Update + Insert)
- undelete
For example, you 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 (such as a record's Id or last Updated field)
and to affect changes in other records, such as logging into an audit table or
firing asynchronous events with a queue.
Trigger.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.
Trigger.
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.
Trigger.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.
Trigger.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.
Summary
Before Insert: new
Before update: old, new, newMap,
oldMap
Before delete: old, oldMap
Before undelete: Similar to Insert
After Insert: new, newMap
After update: old, new, newMap,
oldMap
After Delete: old, oldMap
Point to Remember
·
You cannot delete Trigger.New.
·
Trigger. Old is always read only.
·
Trigger.New and Trigger. Old cannot
be used in Apex DML operations.
Understanding Concepts of Old and New values for different
Events
Important: --
For before events: List of Records, Map of Records did not have Id, Formulas, and
Parent fields.
====================
Before Insert
====================
trigger conceptOldandNew on Contact
(before Insert) {
public Map<Id,String> conIds =
new Map<ID, String>();
system.debug('##### value of trigger.new'+trigger.new);
system.debug('##### value of trigger.old'+trigger.old);
}
=======================
Debug Log Values
=======================
##### ##### value of
trigger.new(Contact:{ OwnerId=00590000000aleEAAQ,Salutation=Mr.,
LastName=test, Email=test@test.com, FirstName=test, IsDeleted=false})
##### value of trigger.old {null}
========================
Before Delete
========================
trigger conceptOldandNew on contact
(before delete) {
public Map<id,String> conIds =
new Map<ID, String>();
system.debug('##### value of trigger.new'+trigger.new);
system.debug('##### value of trigger.old'+trigger.old);
for(Contact con : Trigger.old){
//Create an old and new map so that we
can compare values
Contact oldCon =
Trigger.oldMap.get(con.ID);
system.debug('###### value of
oldMap'+oldCon);
}
}
======================
Debug Log values
======================
##### value of trigger.new {null}
##### value of trigger.old
(Contact:{OwnerId=00590000000aleEAAQ,Salutation=Mr.,
LastName=test,Email=test@test.com, FirstName=test,
Id=0039000000CY9URAA1})
###### value of oldMap Contact:{OwnerId=00590000000aleEAAQ,Salutation=Mr.,
LastName=test,Email=test@test.com, FirstName=test, Id=0039000000CY9URAA1})
====================
After undelete
===================
Important:-- When a record is
deleted it goes into recycle bin. We can undelete a deleted
record from recycle bin and this
caused undelete trigger to fire.
trigger conceptOldandNew on Contact
(before Insert) {
public Map<Id,String> conIds =
new Map<ID, String>();
system.debug('##### value of trigger.new'+trigger.new);
system.debug('##### value of trigger.old'+trigger.old);
}
=======================
Debug Log Values
=======================
##### ##### value of
trigger.new(Contact:{ OwnerId=00590000000aleEAAQ,Salutation=Mr.,
LastName=test, Email=test@test.com,FirstName=test,
IsDeleted=false})
##### value of trigger.old {null}
=====================
Before update
=====================
trigger testEmail on Contact (before
Insert, before update){
public Map<Id,String> conIDs = new Map<Id,String>();
List<Contact>lstContacts= Trigger.New;
Set<Id> setAccId = new Set<Id>();
for(Contact con : lstContacts){
setAccId.add(con.AccountId);
system.debug('###### value of
setAccId'+setAccId);
//Create an old and new map so that we
can compare values
Contact oldCon =
Trigger.oldMap.get(con.ID);
system.debug('###### value of
oldMap'+oldCon);
Contact newCon =
Trigger.newMap.get(con.ID);
system.debug('###### value of
newMap'+newCon);
//Retrieve the old and new Reseller
Email Field
string oldEmail = oldCon.Email;
system.debug('###### value of
oldEmail'+oldEmail);
string newEmail = newCon.Email;
system.debug('###### value of
newEmail'+newEmail);
//If the fields are different, the
email has changed
if(oldEmail != newEmail){
conIDs.put(con.Id,con.Email);
}
system.debug('###### value of
conIds'+conIds);
}
Map<Id, Account> mapAccounts = new Map<Id,Account>([SELECT
Id, Name from Account where Id In:setAccId]);
system.debug('###### value in mapAccounts'+ mapAccounts);
system.debug('###### value of trigger.old'+ trigger.old);
system.debug('####### oldMap'+trigger.newMap.size());
system.debug('####### oldMap'+trigger.oldMap.size());
}
=====================
Debug Log Values
=====================
###### value of setAccId
{0019000000C4pEyAAJ}
###### value of trigger.old
(Contact:{HasOptedOutOfFax=false, OwnerId=00590000000aleEAAQ,
FirstName=test,LastName=Contact,Email=test567@gmail.com,AccountId=0019000000C4pEyAAJ,
Id=0039000000CBHVzAAP})
###### value of trigger.new
(Contact:{HasOptedOutOfFax=false, OwnerId=00590000000aleEAAQ,
FirstName=test,LastName=Contact,Email=test600@gmail.com,AccountId=0019000000C4pEyAAJ,
Id=0039000000CBHVzAAP})
###### value of oldMap
Contact{OwnerId=00590000000aleEAAQ,LastName=Contact,
Experience__c=2.0,LastModifiedById=00590000000aleEAAQ,Email=test567@gmail.com,
AccountId=0019000000C4pEyAAJ, FirstName=test,Id=0039000000CBHVzAAP}
AccountId=0019000000C4pEyAAJ, FirstName=test,Id=0039000000CBHVzAAP}
###### value of newMap
Contact:{OwnerId=00590000000aleEAAQ,LastName=Contact,
FirstName=test,Email=test600@gmail.com, AccountId=0019000000C4pEyAAJ,
Experience__c=2.0, Id=0039000000CBHVzAAP}
Experience__c=2.0, Id=0039000000CBHVzAAP}
###### value of oldEmail test567@gmail.com
###### value of newEmail test600@gmail.com
###### value of conIds {0039000000CBHVzAAP=test600@gmail.com}
###### value in mapAccounts
{0019000000C4pEyAAJ=Account:{Name=test Account, Id=0019000000C4pEyAAJ}}
####### oldMap 1
####### newMap 1
==================
After Insert
==================
//Create a trigger on Account object
to insert a contact, when an account is getting inserted.
//Contact Name will be same as
Account Name
trigger AccountName on Account
(after insert){
List<Contact> contacts = new List<Contact>();
system.debug('##### value of trigger.new'+trigger.new);
system.debug('##### value of trigger.old'+trigger.old);
for(Account acc : Trigger.new){
//Create new map so that we can compare
values
Account newAcc =
Trigger.newMap.get(acc.ID);
system.debug('###### value of
newMap'+newAcc);
}
for (Account a : Trigger.new){
contacts.add(new Contact (lastname =
a.name, AccountId = a.Id));
}
insert contacts;
}
====================
Debug Log values
====================
##### value of trigger.new
(Account:{OwnerId=00590000000aleEAAQ, Name=Poddar,
Id=0019000000CaFDzAAN})
##### value of trigger.old {null}
###### value of newMap
Account:{OwnerId=00590000000aleEAAQ, Name=Poddar, Id=0019000000CaFDzAAN}
====================
After Update
====================
trigger Consetup on Contact (after
insert, after update)
{
system.debug('##### value of Trigger.New'+trigger.new);
system.debug('##### value of Trigger.Old'+trigger.old);
Set<Id> Accid = new Set<Id>();
for (Contact con : Trigger.new){
Accid.add(con.AccountId);
}
Map<Id,Account> accs = new Map<Id,Account>([Select Name, Id
from Account Where Id in : Accid]);
for (Contact con : Trigger.new)
{
accs.get(con.AccountId).Fax = con.Fax ;
//Create an old and new map so that we
can compare values
Contact oldCon =
Trigger.oldMap.get(con.ID);
system.debug('###### value of
oldMap'+oldCon);
Contact newCon =
Trigger.newMap.get(con.ID);
system.debug('###### value of
newMap'+newCon);
}
update accs.values();
}
=======================
Debug Log Values
=======================
##### value of
Trigger.New(Contact:{OwnerId=00590000000aleEAAQ,
Fax=(452)152-1000,LastName=Amul, AccountId=0019000000CaFCDAA3,
Id=0039000000CY9m0AAD})
##### value of
Trigger.Old(Contact:{OwnerId=00590000000aleEAAQ,
Fax=(452)
152-1325, LastName=Amul, AccountId=0019000000CaFCDAA3, Id=0039000000CY9m0AAD})
###### value of
oldMapContact:{OwnerId=00590000000aleEAAQ,LastName=Amul,
Fax=(452) 152-1325, AccountId=0019000000CaFCDAA3,Id=0039000000CY9m0AAD}
Fax=(452) 152-1325, AccountId=0019000000CaFCDAA3,Id=0039000000CY9m0AAD}
###### value of
newMapContact:{OwnerId=00590000000aleEAAQ, Fax=(452) 152-1000, LastName=Amul, AccountId=0019000000CaFCDAA3,Id=0039000000CY9m0AAD}
===================
After Delete
===================
trigger afterTrigger on Account
(after delete){
system.debug('##### value of Trigger.old'+trigger.old);
system.debug('##### value of Trigger.new'+trigger.new);
system.debug('####### oldMap'+trigger.oldMap.size());
for(Account acc : Trigger.old){
List<Contact> contacts = [SELECT Id, AccountId from Contact where
AccountId = :acc.Id Limit 20];
for(Contact con : contacts){
if(contacts.size()>0){
delete contacts;
}
}
}
}
======================
Debug Log Values
======================
##### value of
Trigger.old(Account:{OwnerId=00590000000aleEAAQ,Fax=(525) 325-3233, Name=Test1, Id=0019000000CaEroAAF,Ownership=Public})
##### value of Trigger.new {null}
####### oldMap 1
No comments:
Post a Comment