You need to sign in to do that
Don't have an account?
Jodi Mahoney 18
System.ListException: Duplicate id in list
Please help! I am still a novice when it comes to Apex. I am trying to grab the last activity type and populate it on the lead. The code is working however I am getting duplicate value errors a few times a day and I would like to clean up my code so that doesn't continue to happen.
Can someone take a look and help me out?
Thank you in advance!
Can someone take a look and help me out?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 trigger LastActivityType on Task (after insert,before update) { Set<Id> LeadIds = new Set<Id>(); List<Lead> LeadList = new List<Lead>(); for(Task t :trigger.new) { if(t.whoId!=null) { Schema.SObjectType tType= t.whoId.getSObjectType(); if(tType == Lead.Schema.SObjectType) { LeadIds.add(t.WhoId); } } } { //Querying the related Lead based on whoId on Task Map<Id,Lead> LeadMap = new Map<Id,Lead>([select id,Last_Activity_Type__c from Lead where id in:LeadIds]); for(Task t :Trigger.new) for(Lead l : LeadMap.Values()) { l.Last_Activity_Type__c = t.Type; LeadList.add(l); } } // updating the Lead if(LeadList.size()>0) { update LeadList; } }
Thank you in advance!
Sotos code is not bulky and that will work on single record only . If you will try to update muliple record from data loader then that will not work.
Please try below code and let us know if that will help you
All Answers
you don't have to add the Leads in a list to update them.
You can simply do: and then
Additionally, your code will update the leads for every task so eventually all leads will have the type of the last Task.
You could avoid looping the tasks the second time and just provide the type of the last Task in the trigger.
Hope that helps!
Thank you, I made the changes (see below) and now I am getting an error "Variable does not exist: LeadMap"
I'm guessing I'm miss placed some brackets?
I have updated the code and re arranged it a bit.
Compiles now :)
Let me know if it works :)
Sotos code is not bulky and that will work on single record only . If you will try to update muliple record from data loader then that will not work.
Please try below code and let us know if that will help you