function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RaviKumarRaviKumar 

When an Insert/Update of a field in Contact Object that value needto be update in all remaining Contact records

When an Insert/Update of a field in Contact Object that value needto be update in all remaining Contact records

Below is the Trigger I've written.But when we update a record it throwsan ERROR :Duplicate Id's in List

trigger AccountFieldUpdate on Contact (after Update) {

    List<Contact> C3_l = new List<Contact>();


    for(Contact c1 : Trigger.new){

        Account acc = [select id from Account where id =: c1.AccountId ];
        List<Contact> c2_l = [Select name, AccountId, Department from
         Contact where AccountId =: acc.id ];
       
        for(Contact c2 : c2_l){
        if(c1.id == c2.id){
        }else{
            c2.Department = c1.Department;
            c3_l.add(c2);
           }
        }
    }
    update c3_l;
}
Anup JadhavAnup Jadhav
That's because your update in the 'after update' trigger causes it to invoke the trigger again. Try adding this logic in a before update so you don't have to call update again.