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
Mark LiuMark Liu 

unhandled trigger caused by: System.ListException: Duplicate id in list: 001.............

Hey guys,

Was wondering if someone could help me out… I have built a trigger(below) to count the amount of contacts (that are marked as Primary Liaisons) in each account (of a certain account type) and this number is reported on a custom field that resides on each Account. However, whenever I use dataloader or any other CRM tools to mass update contacts, it gives me this error: “caused by: System.ListException: Duplicate id in list: 001……..” I believe it’s because many accounts of this specific record type have multiple contacts, therefore it was trying to update the same Account over and over again causing duplicate IDs?

Please let me know what I can do to resolve this duplicate ID issue. Help is much appreciated! :)

trigger primaryLiaisonCount2 on Contact(after update) {
LIST<account> accList = new List<account>();
set<String> allAccIds = new Set<String>();

for (Contact c1: trigger.new){
if (c1.Contact_ID_18_Characters__c != null){
allAccIds.add(c1.AccountID);} }

Decimal amount1=[SELECT count() FROM Contact WHERE AccountID IN :allAccIds AND Primary_Liaison__c != null AND Marketing_Status__c != 'Gone From Company' LIMIT 25];

LIST<account> acc1=[SELECT ID FROM Account WHERE ID IN :allAccIds AND recordtypeID ='012000000000j8S' LIMIT 25];

for (Contact c1: trigger.new){

for (Account acc2:acc1){
acc2.Primary_Liaisons__c = amount1;
accList.add(acc2);

}
}
Update accList;
}
Best Answer chosen by Mark Liu
Anoop yadavAnoop yadav
Hi,

Try the below code.
trigger primaryLiaisonCount2 on Contact(after update) {
LIST<account> accList = new List<account>();
LIST<account> accListToUpdate = new List<account>();
Set<Account> accSet = new Set<Account>();
set<String> allAccIds = new Set<String>();

for (Contact c1: trigger.new){
if (c1.Contact_ID_18_Characters__c != null){
allAccIds.add(c1.AccountID);} }

Decimal amount1=[SELECT count() FROM Contact WHERE AccountID IN :allAccIds AND Primary_Liaison__c != null AND Marketing_Status__c != 'Gone From Company' LIMIT 25];

LIST<account> acc1=[SELECT ID FROM Account WHERE ID IN :allAccIds AND recordtypeID ='012000000000j8S' LIMIT 25];

for (Contact c1: trigger.new){

for (Account acc2:acc1){
acc2.Primary_Liaisons__c = amount1;
accList.add(acc2);

}
}
accSet.addAll(accList);
accListToUpdate.addAll(accSet);
Update accListToUpdate;
}

 

All Answers

Anoop yadavAnoop yadav
Hi,

Try the below code.
trigger primaryLiaisonCount2 on Contact(after update) {
LIST<account> accList = new List<account>();
LIST<account> accListToUpdate = new List<account>();
Set<Account> accSet = new Set<Account>();
set<String> allAccIds = new Set<String>();

for (Contact c1: trigger.new){
if (c1.Contact_ID_18_Characters__c != null){
allAccIds.add(c1.AccountID);} }

Decimal amount1=[SELECT count() FROM Contact WHERE AccountID IN :allAccIds AND Primary_Liaison__c != null AND Marketing_Status__c != 'Gone From Company' LIMIT 25];

LIST<account> acc1=[SELECT ID FROM Account WHERE ID IN :allAccIds AND recordtypeID ='012000000000j8S' LIMIT 25];

for (Contact c1: trigger.new){

for (Account acc2:acc1){
acc2.Primary_Liaisons__c = amount1;
accList.add(acc2);

}
}
accSet.addAll(accList);
accListToUpdate.addAll(accSet);
Update accListToUpdate;
}

 
This was selected as the best answer
Mark LiuMark Liu
Thanks so much, Anoop! It worked like a charm and I think I understand what you're trying to do here now :). Thanks for showing me how to dedup account IDs by using SETs! :)