0

I am getting "System.ListException: Duplicate id in list: " in After trigger.How to prevent this.I am trying to update the same record after callout in after trigger.Below is my piece of code:

List<Patient_Package__c> lockPackage = new List<Patient_Package__c>();
if(trigger.new[index].Approval_Status__c=='Approved'){

            lockPackage.add(new Patient_Package__c( id=trigger.new[index].id,ECC_Sync_Status__c=label.PMM_ECC_STATUS_Processing));
            try{
            ECC_protocol_Sub_Callout.ecc_protocol_subscription(string.ValueOf(trigger.new[index].id));
            lockPackage.add(new Patient_Package__c( id=trigger.new[index].id,Approval_Status__c=''));
            }catch(exception e){
                PMM_Exception_DAO.insertExceptionRecord(e,'ECC_Onboarding_Callout','ecccallout');
                system.debug(e.getmessage());
            }
        }
        if(lockPackage!=null && !lockPackage.isEmpty()){
            update lockPackage;
        }
1
  • Of course you are getting this error...you are trying to add the same record to the update list twice and setting two different Approval_Status__c values. Are you trying to clear it out or set it to the Custom Label value? You can't do both.
    – Adrian Larson
    Commented Oct 14, 2016 at 19:05

1 Answer 1

0

In your use case you can easily use map.

map<Id,Patient_Package__c> lockPackage = new map<Id, Patient_Package__c>();
if(trigger.new[index].Approval_Status__c=='Approved'){

            lockPackage.put(trigger.new[index].id,new Patient_Package__c( id=trigger.new[index].id,ECC_Sync_Status__c=label.PMM_ECC_STATUS_Processing));
            try{
            ECC_protocol_Sub_Callout.ecc_protocol_subscription(string.ValueOf(trigger.new[index].id));
            lockPackage.put(trigger.new[index].id,new Patient_Package__c( id=trigger.new[index].id,Approval_Status__c=''));
            }catch(exception e){
                PMM_Exception_DAO.insertExceptionRecord(e,'ECC_Onboarding_Callout','ecccallout');
                system.debug(e.getmessage());
            }
        }
        if( !lockPackage.isEmpty()){
            update lockPackage.values();
        }

This will remove your duplicate Id error.

PS As you are doing this in trigger then you should use before update and write your code there because in this way you need to handle recursive trigger error. And in before update you don't need DML.

1
  • 1
    @tushar-its correct
    – Jack vardy
    Commented Oct 14, 2016 at 19:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .