1

How to update the records in after insert and before update trigger at same time with same field, actually I am struck with the code

trigger PopulateTrigger1 on Contact (before insert,before Update,after insert) {


    if(Trigger.isinsert){
     For(Contact c:Trigger.New){
         if(Trigger.isbefore){
             C.Description='ela';
             C.Department=C.LastName;
            }

            if(Trigger.isafter){
            contact cf=new contact(id=c.id);
            cf.LastName='some bla';
            cf.Description='Having a value';
            cf.Department='one more value';
            update cf;

             }
        }
    }
    else
    {        
    for(contact ce:trigger.new)
        {
            if(Trigger.isbefore){

                ce.phone='123450';
                Ce.Description=ce.FirstName+' '+ce.Phone;
                Ce.Department=Ce.LastName;
                }
            }
            }    
}

While using after insert trigger , before update event is also occurring. Please anyone guide me for the answer.

  • why do you want to update records in after trigger. After trigger are meant for read only operation. Even if you achieve update on after trigger it will trigger another trigger lifecycle for update call. Can you just now move after trigger code in before trigger since you want to perform dml. – Kiran Machhewar Apr 6 '16 at 6:55
  • explain,i am not able to understand – Elavarasan Apr 6 '16 at 7:01
  • I am asking is there any specific reason that you are updating records in after trigger context? – Kiran Machhewar Apr 6 '16 at 7:05
  • it is not possible in both after insert and before update at same time – Elavarasan Apr 6 '16 at 7:06
  • It is possible but it will run your trigger twice. Which can introduce lot of unknown issues because we don't assume that kind of behavior with triggers. As a best practice when you want to update records which are coming in trigger then always should be used before trigger context. – Kiran Machhewar Apr 6 '16 at 7:08
0

I really don't understand your business requirement for doing an update in after insert and suppress update events in trigger, you need to use static variables to do that.

Apex Class:

public class TriggerRecursionStopper {
    public static Boolean isUpdateStopper = False;
}

Trigger:

trigger PopulateTrigger1 on Contact (before insert,before Update,after insert) {           
    if(Trigger.isinsert){
        For(Contact c:Trigger.New){
            if(Trigger.isbefore){
                C.Description='ela';
                C.Department=C.LastName;
            }                  
            if(Trigger.isafter){
                contact cf=new contact(id=c.id);
                cf.LastName='some bla';
                cf.Description='Having a value';
                cf.Department='one more value';
                TriggerRecursionStopper.isUpdateStopper = True; // This will set the static boolean variable to True
                update cf;                              
            }
        }
    }
    else
    {        
        for(contact ce:trigger.new)
        {
            if(Trigger.isbefore){
                if(!TriggerRecursionStopper.isUpdateStopper){ // Check if the static boolean variable is true, if yes skip this, else run this
                    ce.phone='123450';
                    Ce.Description=ce.FirstName+' '+ce.Phone;
                    Ce.Department=Ce.LastName;
                }
            }
        }
    }    
}

Hope it helps.

1

--> First you have to create the following class in your org :

public class checkRecursive {
private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

--> Now use this class in your org as :

trigger contchange on Contact (before insert,before update,after insert) {
   List<Contact> conList=new List<Contact>();
    Set<Id> set1 = new Set<Id>();
    if(Trigger.isbefore){
        if(checkRecursive.runOnce()) {
        For(Contact c:Trigger.New){
            if(Trigger.isInsert){
                C.Description=c.FirstName;
                C.Department=C.LastName;
            }
            if(Trigger.isUpdate){
                c.phone='123450';
                C.Description=c.FirstName+' '+c.Phone;
                C.Department=C.LastName;
            }
        }
        }
    }
     else    
     {
         for(contact ce:trigger.new)
         {
            contact cf=new contact(id=ce.id);
            cf.LastName='some bla';
            cf.Description='Having a value';
            cf.Department='one more value';
            update cf;
         }
     }

}

Result On Update Regards,

Ajay

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.