+ Start a Discussion
raghava520raghava520 

problem with recursive trigger

Hi Experts i am writing a trigger on account object if  create contact checkbox is checked  then  it should create the contact with the same values given in  account  such as account name ,phone etc

here is my trigger

trigger accctrg on Account (after insert,after update) {
   Account acc = trigger.new[0];
    if(recclass.isrecursive = true){
         recclass.isrecursive=false;       
         account ac = new account(id= acc.Id);
        if(acc.create_contact__c== true){
   Contact con = new Contact();
   con.LastName = ac.Name;
   con.Phone = ac.Phone;
   con.AccountId = acc.Id;
             update con;
         }
       }
}
trigger class to avoid recursion

public with sharing class recclass {
public static Boolean Isrecursive = true;
}

 iam not getting any errors but my code is not working
 Regards
 Raghava
sfdc_ninjasfdc_ninja
It looks like you are trying to updatethe contact instead of inserting it.  Try to change

update con;

to 

insert con;

On another note, while using static booleans to control recursion is a good practice, this trigger is not bulkified and you might want to look inot that as well as this will only work on a single record.