Professional Documents
Culture Documents
Sal Se Force
Sal Se Force
1. What is Trigger?
Trigger in Salesforce is essentially an Apex script used by developers before or after events
related to data manipulation language (DML). Apex triggers enable you to perform custom
actions before or after changes to Salesforce records, such as insertions, updates, or
deletions. Apex trigger gets executed when you perform a DML operation either from UI or
Apex.
1st Way:-
In Developer Console → File → New → Apex Trigger → Write name of trigger
and select sObject.
2nd Way:-
Inside Object Manager → Select Object on which you want to create trigger
→ On the left pane select trigger → Click new button
3. Trigger Syntax
A trigger is a set of statements that can be executed on the following events. In trigger
events, one or more of the below events can be used with comma-separated.
● before insert
● before update
● before delete
● after insert
● after update
● after delete
● after undelete
● Before triggers are used to perform a task before a record is inserted or updated or
deleted. These are used to update or validate record values before they are saved to
the database.
● After triggers are used if we want to use the information set by the Salesforce
system and to make changes in the other records. are used to access field values
that are set by the system (such as a record’s Id or LastModifiedDate field), and to
affect changes in other records. The records that fire the after the trigger is read-only.
● isExecuting: Returns true if the current context for the Apex code is a trigger, not a
Visualforce page, a Web service, or an executeanonymous() 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 the API.)
● new: Returns a list of the new versions of the 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: A map of IDs to the new versions of the sObject records. This map is only
available in before update, after insert, after update, and after undelete triggers.
● old : Returns a list of the old versions of the sObject records. This sObject list is only
available in update and delete triggers.
● oldMap: A map of IDs to the old versions of the sObject records. This map is only
available in update and delete triggers.
● size: The total number of records in a trigger invocation, both old and new.
System.debug('newList : '+newList);
System.debug('oldList : '+oldList);
}
Trigger.newMap contains new values of record on value side and id on key side.
Trigger.oldMap contains old values of record on value side and id on key side.
Both of the above have return type of map. Please look at the below code:-
trigger OpportunityTrigger on Opportunity (before update) {
System.debug('newMap : '+newMap);
System.debug('oldMAp : '+oldMAp);
}
8. Sizes:-
Assignment:-
2. What is Trigger.isExecuting?
If you are calling any apex class from trigger then Trigger.isExecuting returns true.
Suppose take a look at below class:-
OpportunityTrigger.apxt
OpportunityTriggerHelper.myMethod();
}
OpportunityTriggerHelper.apxc
if(Trigger.isExecuting) {
System.debug('Trigger Code');
} else {
System.debug('Non Trigger Code');
}
}
}
if(cityName == 'Pune')
{
System.debug('I am inside pune');
}
else if(cityName == 'Nasik')
{
System.debug('I am inside Nasik');
}
else if(cityName == 'Mumbai')
{
System.debug('I am inside Mumbai');
}
else
{
System.debug('I am inside other city');
}
}
}
switch on cityName {
when 'Pune' {
System.debug('I am inside pune');
}
when 'Nasik' {
System.debug('I am inside Nasik');
}
when 'Mumbai' {
System.debug('I am inside Mumbai');
}
when else {
System.debug('I am inside other city');
}
}
}
}
● An enum is an abstract data type with values that each take on exactly one of
a finite set of identifiers that you specify. Enums are typically used to define a
set of possible values that don’t otherwise have a numerical order, such as
the suit of a card, or a particular season of the year.
● The easy way to think of them are compile-time “constants” that can be
statically typed. They’re sort of like a list of static strings, in that sense, and
they obey some interesting rules in addition to the basics:
● All enums have access to two methods: name() and ordinal() - name simply
returns the string version of your enum instance (the equivalent of toString for
enums), ordinal returns its index in the “list” — FirstValue would have an index
of 0 in the above example; SecondValue would have an index of 1.
5. Enum Example:-
trigger AccountTrigger on Account (before update, before insert, before delete, after
update, after insert, after delete, after undelete) {
switch on Trigger.operationType {
when BEFORE_UPDATE {
when BEFORE_INSERT {
when BEFORE_DELETE {
when AFTER_UPDATE {
//Write after update logic here
}
when AFTER_INSERT{
when AFTER_DELETE{
when AFTER_UNDELETE {
7. Points to Remember
AccountTrigger.apxt
if(Trigger.isBefore) {
if(Trigger.isInsert) {
AccountTriggerHandler.typeProspect(Trigger.new);
}
}
AccountTriggerHandler.apxc
if(acc.Rating == 'Cold') {
acc.Type = 'Prospect';
}
}
}
}
AccountTrigger.apxt
if(Trigger.isBefore) {
if(Trigger.isInsert || Trigger.isUpdate) {
AccountTriggerHandler.ratingValidation(Trigger.new);
}
}
}
AccountTriggerHandler.apxc
if(acc.Rating == null) {
acc.addError(Account.Rating, 'Rating cannot be empty');
}
}
}
}
AccountTrigger.apxt
if(Trigger.isBefore) {
if(Trigger.isUpdate) {
AccountTriggerHandler.isActiveCheck(Trigger.new, Trigger.old);
}
}
}
AccountTriggerHandler.apxc
if(accNew.Id == accOld.Id) {
if(accNew.Email__c != accOld.Email__c) {
accNew.isActive__c = true;
}
}
}
}
}
}
AccountTriggerHandler.apxc
if(accNew.Email__c != accOldMap.get(accNew.Id).Email__c) {
accNew.isActive__c = true;
}
}
}
}
Assignment:-
1. Write a trigger such that when student enters his mobile no,
then make sure that mobile no is of 10 digits.
StudentTrigger.apxt
if(Trigger.isBefore) {
if(Trigger.isInsert || Trigger.isUpdate) {
StudentTriggerHandler.mobileValidation(Trigger.new);
}
}
}
StudentTriggerHandler.apxc
OpportunityTrigger.apxt
if(Trigger.isBefore) {
if(Trigger.isDelete) {
OpportunityTriggerHandler.preventDeletion(Trigger.old);
}
}
}
OpportunityTriggerHandler.apxc
AccountTrigger.apxt
if(Trigger.isBefore) {
if(Trigger.isUpdate) {
AccountTriggerHandler.preventUpdate(Trigger.new);
}
}
}
AccountTriggerHandler.apxc
Assignment:-
Note:- In this trigger when you write before update run the
trigger only when the stage name will be changed and it must
be Closed Won or Closed Lost. Write two separate methods
one method before insert and another method before update
and call these methods using the appropriate way from the
trigger.
2. Create a custom object Calculator. Create below fields on the
object:-
● Number 1
● Number 2
● Addition
● Subtraction
● Multiplication
● Division
All the above fields must have a number data type when
creating these fields. (Do not create formula fields here)
Write a trigger such that Number 1 and Number 2 will be
required fields. If Number 1 and Number 2 are not blank then
Addition, Subtraction, Multiplication and Division fields must
gets calculated based on Number 1 and Number 2.
If you have any questions regarding this please ping me once.