Trigger - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Pooja SharmaPooja Sharma 

Trigger

Whenever Account record is updated, before updating the record create a new record in contact object with old values of the account record.

trigger UpdateConwithOldValuesofAcc on Account (before update) 
{
    List<Contact> c=new List<Contact>();
   for(Account acc:Trigger.old)
   {
       contact con=new contact();
          con.AccountId=acc.Id;
          con.LastName=acc.Name;
          con.Phone = acc.Phone;
          
       c.add(con);
   }
    insert c;
}

Getting Error while updating Account .
Any suggestion will be appreciated.
Thank you
Best Answer chosen by Pooja Sharma
Moustafa IshakMoustafa Ishak
There is a recursive call, that's why it fails could you follow point 4 in the below link 

https://help.salesforce.com/articleView?id=000332407&mode=1&sfdcIFrameOrigin=null&type=1

let me know if you need further assistance or it works 😄

All Answers

Gaurav SharmaGaurav Sharma
can you post the error you are getting?
Moustafa IshakMoustafa Ishak
Hi Pooja,

Could you please make it as after update
trigger UpdateConwithOldValuesofAcc on Account (after update) 
{
    List<Contact> c=new List<Contact>();
   for(Account acc:Trigger.old)
   {
       contact con=new contact();
          con.AccountId=acc.Id;
          con.LastName=acc.Name;
          con.Phone = acc.Phone;
          
       c.add(con);
   }
    insert c;
}
Kindly let me know if it works :D

Thanks
Moustafa Ishak
 
Pooja SharmaPooja Sharma
I did' After update' but getting error
Pooja SharmaPooja Sharma
Error:Apex trigger UpdateConwithOldValuesofAcc caused an unexpected exception, contact your administrator: UpdateConwithOldValuesofAcc: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccPhoneOnConCreation: maximum trigger depth exceeded Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert: []: Trigger.UpdateConwithOldValuesofAcc: line 14, column 1
Pooja SharmaPooja Sharma
This is the error that I'm getting
Vivek HVivek H
Hi Pooja,

Your code should work fine. Same code works fine in my dev org. There must be other dependencies causing error. Can you share the exception message?
Moustafa IshakMoustafa Ishak
There is a recursive call, that's why it fails could you follow point 4 in the below link 

https://help.salesforce.com/articleView?id=000332407&mode=1&sfdcIFrameOrigin=null&type=1

let me know if you need further assistance or it works 😄
This was selected as the best answer
Pooja SharmaPooja Sharma
Thank you all of you, because of two trigger on same object it was showing error.