You need to sign in to do that
Don't have an account?
Mamadou Diallo 14
Error: System.ListException: Duplicate id in list
Hello,
I have trigger that will update a custom field Segment__c on the account based on the owner Role Description. I'm getting this error when I change the owner of the account to someone in the Operations department:
Here is the part of the trigger that is not working:
Thank you for your help.
I have trigger that will update a custom field Segment__c on the account based on the owner Role Description. I'm getting this error when I change the owner of the account to someone in the Operations department:
Error: Apex trigger SegmentRulesTrigger caused an unexpected exception, contact your administrator: SegmentRulesTrigger: execution of AfterUpdate caused by: System.ListException: Duplicate id in list:
Here is the part of the trigger that is not working:
trigger SegmentRulesTrigger on Account (after insert, after update) { //Create a list of accounts to update List<Account> acctsListToUpdate = new List<Account>(); //check if trigger is running again if(RecursiveTriggerHandler.runOnce()){ for (Account a : Trigger.new) { //Query the owner role description for(User u : [SELECT id, Role_Description__c FROM User WHERE id = :a.OwnerId]){ // Check if the account ID is not null // to avoid "referencing null object" error if(a != NULL && a.ID !=NULL){ // create a placeholder account a1 Account a1 = new Account(Id = a.Id, Name = a.Name, OwnerId = a.OwnerId); //Map Account ID Account oldAcct = Trigger.oldMap.get(a1.Id); //Check if Account Owner or Name has changed if (oldAcct.OwnerId != a1.OwnerId || oldAcct.Name != a1.Name){ // User u1 = new User(Id= u.Id, Role_Description__c = u.Role_Description__c); //Check if Owner role description is Operations if( u.Role_Description__c == 'Operations' ) { a1.Segment__c = 'Other'; acctsListToUpdate.add(a1); } a1.Segment__c = u.Role_Description__c; acctsListToUpdate.add(a1); } } //Bulkify the update if(acctsListToUpdate.size()>0){ update acctsListToUpdate; } } } } }
Thank you for your help.
Hi,
Please try below compact and refined code:
All Answers
Thank you for taking the time to work on my issue. I don't see difference between your code and mine. Can you point me to the changes you made to my code?
Thanks again.
hi,
pls try like this..
before updating put it in a set and then again in a list,and then update.
pls try like this. Let me know if this does not work.
Thanks,
Manohar
You are getting this error because you are inserting account 2 times into a list in a single loop. So, when you update the list having duplicate ID will cause an issue.
You should add else to avoid this in your code
if (oldAcct.OwnerId != a1.OwnerId || oldAcct.Name != a1.Name){
// User u1 = new User(Id= u.Id, Role_Description__c = u.Role_Description__c);
//Check if Owner role description is Operations
if( u.Role_Description__c == 'Operations' )
{
a1.Segment__c = 'Other';
acctsListToUpdate.add(a1);
}
else{
a1.Segment__c = u.Role_Description__c;
acctsListToUpdate.add(a1);
}
}
}
Hi Mamadou,
in which line you are getting null pointer, can you pls post your code..
Thanks,
Manohar
Hi,
Please try below compact and refined code:
When I edit a single record, I don't have errror.
When I create a new account, I have this error I used Data Loader to bulk reassign accounts (batch 200), I got this error
Deepark, I'm trying your code. I will keep you updated.
Hi,
Yes please try my code and keep me posted if any issue arises
Thank you Manohar for your help.
Hi,
Can you please let me know how many triggers are there on account?
Ideally there should be only 1 trigger on 1 object.
You need to merge all the triggers in 1 and put all three triggers logic in 1 handler class.
CPU time limit error generally comes when nested for loops are there. Please avoid them and use Map in place of them.
You can share all three triggers on below email address:
dpkm20@gmail.com