0% found this document useful (0 votes)
3 views

Triggers

Triggers in Apex are executed during DML operations to validate transactions and automate actions. They can be categorized into Before Triggers, which validate data before saving, and After Triggers, which automate actions after data is saved. Trigger Context Variables provide runtime information about the records involved in the triggering event, allowing for dynamic responses based on the operation performed.

Uploaded by

vamsivoonna8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Triggers

Triggers in Apex are executed during DML operations to validate transactions and automate actions. They can be categorized into Before Triggers, which validate data before saving, and After Triggers, which automate actions after data is saved. Trigger Context Variables provide runtime information about the records involved in the triggering event, allowing for dynamic responses based on the operation performed.

Uploaded by

vamsivoonna8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Trigger

Trigger is a piece of apex code, that's get executed or fired whenever appropriate
DML Operation is performed.
(Data Manipulation Language -> Insert, Update, Delete and Undelete)

-> Trigger depends on DML Statements.

-> No. of ways we can make the field as required :


1) Create a field 2) Page Layout 3) Lightning Record Page 4)Flows
5)Validations 6) Triggers

-> Trigger is a combination of "Validation" + "Automation(Trigger Flows,


Triggers)".

Why we need Triggers ?


Ans) To restrict the transaction

It is used for two purposes :


1) Validation of a transaction. (If you want to stop the transaction with the
validation then you can go with Trigger)
2) Automating the actions after the transaction.

Ex : There are 3 Objects A , B , C .


A is a parent object to object B.
C Object is a Non-Related object to both objects A & B.

whenever the record gets inserted/Deleted/UnDeleted/Updated in object B then


we can changes values in object A by workflows/Flows/Process Builders because A
and B objects are in Relationships.

If any record gets Inserted/Deleted in object C, then i want to change the values in
object A, then it is not possible with the Workflows / Process Builder because the
objects C and A are not in Relationships, But it is possible with the help of
Triggers. Even though they are in Relationships or Individuals still whatever action
you can do.

What are Trigger Events?


For Every action in record, there will be multiple states of “Before” & “After”.

There are two types of events in triggers :


i) Before Trigger
ii) After Trigger

 Before Triggers are gets fired before saving the data to database (Model) .
 After Triggers are gets fired after inserting data to database (Model) .
Before Trigger :
Before Triggers works like a Validation. (If you want to perform Validation, then
you can go with "Before Trigger")

If Base Object(object on which trigger defined) and Action/Related Object(object


that triggers the action) is same, then you can go with "Before Trigger".
(operate on the same object where the action is occurring.)

If you want to work with single object, then you can go with "Before Trigger".

Ex : i have written a trigger for Account object, whenever Account gets Inserted/
Deleted/ Updated/ UnDeleted , then it should raise an Validation error
message has You are not supposed to perform this action because you are
not author / owner for the Object ).

After Trigger :
After Triggers works like a Automation.(If you want to perform an Action /
Automation, then you can go with "After Trigger")

If Base Object(object on which trigger defined) and Action/Related Object(object


that triggers the action) are different, then you can go with "After Trigger".
(Operate on a different object other than where the action is occurring.)

If you want to work with one or more objects, then you can go with "After
Trigger".

Ex : whenever you are going to Insert/ Delete/ UnDelete/ Update in Account


object, then
there should be Action should done in Contact object. For that you can go
with
"After Trigger".

NOTE :
Transaction : It represents a set of operations that are executed as a single unit.

 The Apex Runtime Environment is responsible to executing Triggers.


 For a trigger to get executed, the Apex RunTime Environment provides
information about records or data involved in the triggering event.
(the current variables that are going to be inserted/updated/deleted)

 To supplies this information, the Apex RunTime Environment creates and


Loads runtime variables known as "Trigger Context Variables".

 All Trigger Context Variables are Boolean type.


 The triggers only contain the variables which are participating in the DML
event.

NOTE :
Apex RunTime Environment supplies the information to Triggers.

Trigger Syntax :
Trigger trigger_name on Object_Name()
{
//block of code
}

What are Trigger Context Variables ?


These are the variables created and Loaded in runtime by Apex RunTime
Environment to supply the information about the events and data under
processing for an appropriate triggers.

(Trigger Context Variables stores the values temporary and that can
be carried from one field to another field or one Object to another
object.)

These variables are divided into 2 types :


A) (Event Based)
isExecuting : Returns true, if the current context of Apex code is a
trigger, not a
Visualforce page, a Web service, or an executeanonymous()
API call.

isInsert : Returns true, if trigger was fired due to an insert operation, fro
the Salesforce UI, Apex or API.

isUpdate : Returns true, if trigger was fired due to an update operation,


fro the Salesforce UI, Apex or API.

isDelete : Returns true, if trigger was fired due to an delete operation,


from the Salesforce UI, Apex or API.

isUndelete : Returns true, if the trigger was fired after a record is


recovered from Recycle Bin.

isUpsert : Returns true, if the trigger was fired due to record upsert.

isBefore : Returns true, if the trigger was fired before any record was
saved.

isAfter : Returns true, if the trigger was fired after all records were saved.

B) (Data Based)
new (Trigger.New) : Returns a list of new versions of sObject records.
This sObject list is available in Insert, Update and
Undelete triggers, and the records can only be
modified in before trigger.
newMap : A map of ids to the new versions of sObject records.
Available in after insert, before, update, after update, after
undelete triggers.

old : Returns a list of old versions of sObject records.


Available in before update, after update, before delete, after delete
triggers.

oldMap : A map of ids to the old versions of sObject records.


Available in before update, after update, before delete, after delete
triggers.
size : The total number of records in a trigger invocation, both old and
new.

All these events are Boolean type.

Example :

-> Before Insert :


1) when you create a new Account record , then Description field should automatically populate with
“Test Description”.
Trigger AccountTrigger on Account(before insert)
{
for(Account acc : Trigger.New)
{
acc.Description = ‘Test Description’;
}
}

When Creating a new Account record with some Name and save it. Then it will automatically populate
the description field value has “Test Description”.

2) If Account industry is not null and having value as ‘Media’ then populate Rating as hot.

Trigger AccountTrigger on Account (before insert) {


for (Account acc : Trigger.new) {
if (acc.Industry != null && acc.Industry == 'Media') {
acc.Rating = 'Hot';
}
}
}

After Insert : Create related opportunity when Account is created.


( To create relate opportunity record we need account record id )

Trigger AccountTrigger on Account (after insert) {


List<Opportunity> opportunitiesToInsert = new List<Opportunity>();
for (Account acc : Trigger.new) {
opportunitiesToInsert.add(new Opportunity(
Name = 'Opportunity related to Account',
AccountId = acc.Id
));
}
insert opportunitiesToInsert;
}

You might also like