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
Kr ramKr ram 

Duplicate Id in list-Even though code bulkified

Hello gurus,

I am trying to perform a mass update using the data loader,and the below code is throwing duplicate IDs error,
i know i am using SET ,but any pointers on using MAP so that i can fix the error.
All i am doing is copying 2 fields from child(appl__c) to parent(contact)  where the parent is a lookup field in the child table.
Any pointers? thank you!

trigger abre on Appl__c (after insert, after update) {

    Set<Contact> parents = new Set<Contact>();
  
    for (Appl__c child : trigger.new) {
        if(child.Id != null){
            parents.add(new Contact(Id = child.Contact__c, Admit_Type__c = child.Admit_Type__c,Admit_Term__c=child.Admit_Term__c));
        }
    }

    if(!parents.isEmpty()){
       
        List<Contact> toUpdate = new List<Contact>();
        toUpdate.addAll(parents);
        update toUpdate;
    }
    }
Himanshu ParasharHimanshu Parashar
Hi Ram,

Try following code
 
trigger abre on Appl__c (after insert, after update) {

    set<id> ContactIds = new Set<id>();
    Set<Contact> parents = new Set<Contact>();
  
    for (Appl__c child : trigger.new) {
        if(child.Id != null){

           if(!ContactIds.contains(child.contact__c))
           {
              ContactIds.add(child.contact__c); 
              
               parents.add(new Contact(Id = child.Contact__c, Admit_Type__c =                  
               child.Admit_Type__c,Admit_Term__c=child.Admit_Term__c));

           }
        }
    }

    if(!parents.isEmpty()){
       
        List<Contact> toUpdate = new List<Contact>();
        toUpdate.addAll(parents);
        update toUpdate;
    }
    }

 
Kr ramKr ram
Hi Himanshu,

i am still testing this updated one,but wondering why the one i posted does not work for bulk updates?
thank you again!