You need to sign in to do that
Don't have an account?
Sainath Reddy
Apex trigger TaskUpdateLead caused an unexpected exception, contact your administrator: TaskUpdateLead: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 00Q0S000002IXn2UAG: ()
Hello
I'am getting this error on the below trigger can anyone help me in solving this :Apex trigger TaskUpdateLead caused an unexpected exception, contact your administrator: TaskUpdateLead: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 00Q0S000002IXn2UAG: ()
trigger TaskUpdateLead on Task ( After insert) {
Set<ID> LeadIds = new Set<ID>();
//We only care about tasks linked to Leads.
String leadPrefix = Lead.SObjectType.getDescribe().getKeyPrefix();
//Add any Lead ids coming from the new data
if(trigger.new!=null){
for (Task t : Trigger.new) {
if (t.WhoId!= null && string.valueof(t.WhoId).startsWith(leadPrefix) ) {
if(!LeadIds.contains(t.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t.WhoId);
}
}
}
}
//Also add any Lead ids coming from the old data (deletes, moving an activity from one Lead to another)
if(trigger.old!=null){
for (Task t2 : Trigger.old) {
if (t2.WhoId!= null && string.valueof(t2.WhoId).startsWith(leadPrefix) )
{
if(!LeadIds.contains(t2.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t2.WhoId);
}
}
}
}
if (LeadIds.size() > 0){
List<Lead> leadsWithTasks = [select id,Activity_Count__c,Last_activity_date__c,(select id,createddate from Tasks) from Lead where Id IN : Leadids];
List<Lead> leadsUpdatable = new List<Lead>();
//set<ID> LeadIds = new Set<ID>();
for(Lead L : leadsWithTasks){
If(L.Last_activity_date__c == NUll || L.Last_activity_date__c != System.Today())
{
Set<Date> set_Uniquedate = new Set<Date>();
integer Count = 0;
For(Task tsk: L.tasks)
{
If(!set_Uniquedate.Contains(date.newinstance(tsk.createddate.year(), tsk.createddate.month(), tsk.createddate.day())))
{
Count = Count+1;
L.Last_activity_date__c = System.Today();
L.Activity_Count__c = Count;
leadsUpdatable.add(L);
set_Uniquedate.add(date.newinstance(tsk.createddate.year(), tsk.createddate.month(), tsk.createddate.day()));
}
}
}
}
if(leadsUpdatable.size()>0){
update leadsUpdatable;
//update all the leads with activity count
}
}
}
I'am getting this error on the below trigger can anyone help me in solving this :Apex trigger TaskUpdateLead caused an unexpected exception, contact your administrator: TaskUpdateLead: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 00Q0S000002IXn2UAG: ()
trigger TaskUpdateLead on Task ( After insert) {
Set<ID> LeadIds = new Set<ID>();
//We only care about tasks linked to Leads.
String leadPrefix = Lead.SObjectType.getDescribe().getKeyPrefix();
//Add any Lead ids coming from the new data
if(trigger.new!=null){
for (Task t : Trigger.new) {
if (t.WhoId!= null && string.valueof(t.WhoId).startsWith(leadPrefix) ) {
if(!LeadIds.contains(t.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t.WhoId);
}
}
}
}
//Also add any Lead ids coming from the old data (deletes, moving an activity from one Lead to another)
if(trigger.old!=null){
for (Task t2 : Trigger.old) {
if (t2.WhoId!= null && string.valueof(t2.WhoId).startsWith(leadPrefix) )
{
if(!LeadIds.contains(t2.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t2.WhoId);
}
}
}
}
if (LeadIds.size() > 0){
List<Lead> leadsWithTasks = [select id,Activity_Count__c,Last_activity_date__c,(select id,createddate from Tasks) from Lead where Id IN : Leadids];
List<Lead> leadsUpdatable = new List<Lead>();
//set<ID> LeadIds = new Set<ID>();
for(Lead L : leadsWithTasks){
If(L.Last_activity_date__c == NUll || L.Last_activity_date__c != System.Today())
{
Set<Date> set_Uniquedate = new Set<Date>();
integer Count = 0;
For(Task tsk: L.tasks)
{
If(!set_Uniquedate.Contains(date.newinstance(tsk.createddate.year(), tsk.createddate.month(), tsk.createddate.day())))
{
Count = Count+1;
L.Last_activity_date__c = System.Today();
L.Activity_Count__c = Count;
leadsUpdatable.add(L);
set_Uniquedate.add(date.newinstance(tsk.createddate.year(), tsk.createddate.month(), tsk.createddate.day()));
}
}
}
}
if(leadsUpdatable.size()>0){
update leadsUpdatable;
//update all the leads with activity count
}
}
}
I think in after insert we cannot use trigger.old.
Please remove below code and check once :
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog : http://salesforceprasad.blogspot.com/