Trigger is piece of code that is executes before and after record is Insert/update/Deleted from the force.com database.
There are two types of Triggers:
1.Before trigger are used to update or validate record values before they're saved to the
database.
2.After triggers are used to access field values that are set by the system and to effect changes in other records.The records that fire the after trigger are read-only.
syntax:
========
trigger triggerName on ObjectName(trigger_events)
{
//code_block
}
where trigger_events can be a comma-separated list of one or more of the following events :
before insert
before update
before delete
after insert
after update
after delete
after undelete
ex :
========
check email null or not before insert update
record
trigger CheckEmail on contact(before Insert,before update)
{
for(Contact c:Trigger.new)
{
if(c.Email ==Null)
{
c.Email.addError('Please insert Emil ID);
}
}
}
Trigger Context Variables :
=====================
All triggers define implicit variables that allow developers to access run-time context.
Trigger.isInsert : Returns true if this trigger was fired due to an insert operation.
Trigger.isUpdate : Returns true if this trigger was fired due to an update operation
Trigger.isDelete : Returns true if this trigger was fired due to delete operation.
Trigger.isBefore : Returns true if this trigger was fired before any record was saved.
Trigger.isAfter : Returns true if this trigger was fired after all records were saved.
Trigger.isUndelete : Returns true if this trigger was fired after a record is recovered from the
Recycle Bin.
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.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.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.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.
Trigger.size: The total number of records in a trigger invocation,both old and new.
Trigger.isExecuting : Returns true if the current apex code is a trigger.
ex :
trigger Opportunity_trigger on Opportunity(before insert,before update){
for(Opportunity p : Trigger.new)
{
if(Trigger.isInsert && p.amount<10000)
{
p.addError("amount is less than 10000');
}
else if(Trigger.IsUpdate && p.Amount<20000)
{
p.addError('Amount is less than 20000');
}
}
}
ex :
public class trigger_class{
public static void trigger_method(List<Opportunity> oppt){
Double Total_amount=0;
for(Opportunity o : Select Amount from opportunity where createddate=Today()
and CreatedById= : UserInfo.getUserID()]){
Total_amount=Total_Amount +o.Amount;
}
for(Opportunity o1 : oppt){
Total_amount=Total_Amount +o1.Amount;
if(Total_amount>100000)
{
01.addError('you have exceeded your daily limit');
}
}
}
}
trigger t1 on Opportunity(before Insert){
trigger_class.trigger_method(Trigger.new);
}
No comments:
Post a Comment