Amazon

Trigger in salesforce

Trigger is a automated process which is executed whenever a Dml operations occurs.

 Dml operations

   - insert
   - update
   - Delete
   - Undelete

Triggers are divided into two types.


    Before Triggers

    After Triggers

Before Triggers;-

Before triggers can be used to update or validate values of a record before they are saved to the database.

After Triggers;-

After triggers can be used to access field values of the records that are stored in the database and use this values to make changes in other records.


syntax;-


   Trigger triggerName on objectName(Trigger_events){

       code_block
    }

 Where triggers_event can be comma seperated list of events.


Types of events in the triggers;-



  1. Before insert
  2. Before update
  3. After insert
  4. After Update
  5. Before Delete
  6. After Delete
  7. After undeletr

Eg;-
 
   trigger sample on account(before insert,before update)
 
    {
        // code_block

     }

Triggers context Variables;-


These variable are defined in system.trigger class


 Trigger.New;- 

This stores list of new record of sobjects which we are trying to insert into database.This variable is available in triggers which perform insert events and update events.



Trigger.NewMap;- 


This is a map of IDs as keys the new version of the sobject record as value Map<ID,Sobject>  this will available only in after insert , after update and before update triggers.


Triger.Old;- 


Returns a list of the old version of the sobject records( This will store the list of old records with old values what it has before the operation). This is available in before update,after update,before delete and after delete.


Trigger.oldMap;- 


A map of IDs to the old versions of the sobjects records,This map is only available in update and delete Triggers.


Triggers.isExecuting;- 


Returns true if the current context for the Apex code is trigger.


Trigger.isinsert;- 


Returns true if this trigger was fired due to an insert operation from the salesforce user interface apex.


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 a delete operation from the salesforce.



Scenario1;-


when we are trying to insert new record into object if there is any record existing with same account name it should prevent duplicate record.


trigger accountinsert on account (before insert){

 set<string> names = new set<string>();
 Map<string,account> map1= new map<string,account>();

     for(account a: trigger.new)
            {
                 names.add(a.name);
             }
List<account> acc=[select id,name from account where name =:names];

            for(account a: acc){

              map1.put(a.name,a);
             }

           for(account a: trigger.new) {

             list <account> a1= map1.get(a.name);

             if(a1!=null){

               a.name.adderror('you cannot create duplicate account');
               }

           }
     }

No comments