Triggers
Triggers
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)
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.
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 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 you want to work with one or more objects, then you can go with "After
Trigger".
NOTE :
Transaction : It represents a set of operations that are executed as a single unit.
NOTE :
Apex RunTime Environment supplies the information to Triggers.
Trigger Syntax :
Trigger trigger_name on Object_Name()
{
//block of code
}
(Trigger Context Variables stores the values temporary and that can
be carried from one field to another field or one Object to another
object.)
isInsert : Returns true, if trigger was fired due to an insert operation, fro
the Salesforce UI, Apex or API.
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.
Example :
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.