Skip to main content

Hi, I am new to this.

what i am trying to do is make a trigger in which acc_number__c is updated in Contact from the account field acc_number__c if the checkbox field final_number__c is selected in the contact object.

I was confused as to use after or before for this.

i tried with after as follows :

trigger Accnumberupdate2 on contact (after insert ) {

 

            

 

            list<contact> conlist = new list<contact>();

 

            list<contact> con = new list<contact>();

 

           Set <id> ids = new set<id>();

 

       for(contact c : trigger.new){

 

              if(c.Finalize_Number__c == true){

 

                system.debug('If condition');

 

                ids.add(c.Id);

 

                              }

 

       }

 

            con = [select acc_number__c,finalize_number__c,account.acc_number__c from contact where id

 

                    in :ids];

 

           

 

                

 

        for(contact c : con){

 

               

 

                c.Acc_Number__c = c.account.acc_number__c;

 

                conlist.add(c);

 

 

        

 

             }

 

    

 

    

 

 update conlist;   

 

 

}

the above code worked.

but when i use the below code with before, it doesnt work, the field doesnt get updated.

trigger Accnumberupdate3 on contact (before insert ,before update) {

 

          

 

    acc_number__c,finalize_number__c,account.acc_number__c from contact where id in :ids];

 

                

 

        for(contact c : trigger.new){

 

                system.debug('for condition'+ c.Acc_Number__c);

 

            if(c.Finalize_Number__c == true){

 

             c.Acc_Number__c = c.account.acc_number__c;

 

         

 

                    }

 

        

 

               }

 

    

 

    

 

    

 

 

}

If anyone can please tell what is the problem with before trigger and why is it not working ,that would be great because i feel logically it is fine.I think i need to understand when to use before and after and this example could make me understand a little.

Thanks

4 answers
  1. May 18, 2017, 1:14 PM

    Oh ok,sorry i didnt know.

    Thanks for the link though, can you please help me how to post in that forum please,I am not able to find where the link to post a question is,it would be really be helpful if you could tell me where it is.

    No problem if you can't.

    .Cheers,

    Nitin

0/9000