+ Start a Discussion
venkateshyadav1243venkateshyadav1243 

System.ListException: Duplicate id in list: 00T9000000O8CnMEAV

Hi

i written one schedular class assigning task to users

am getting this error can any one tell me why am getting

this schedular class worked me for few days but after some days its showing this error

 

System.ListException: Duplicate id in list: 00T9000000O8CnMEAV

 

this is my class.

 

global with sharing class Task_Escalation_duedate implements Schedulable
 {

public List<Client_Requirement__c> clientreq=new list<Client_Requirement__c>();
public List<Client_Requirement__c> clientreq1=new list<Client_Requirement__c>();
public List<Client_Requirement__c> clientreq2=new list<Client_Requirement__c>();
public list<task> listtask=new List<task>();
public list<task> listtask2=new list<task>();
public list<task> listtask3=new List<task>();
public list<task> listtask4=new List<task>();
public list<task> crmtasks=new List<task>();
public list<task> crmtasks1=new List<task>();
public list<user> userrecords=new List<user>();
public set<id> listtask1=new set<id>();
public set<id> CRID=new set<id>();
public set<id> CRMID=new set<id>();
 
public set<id> sysid=new set<id>();


global void execute(SchedulableContext SC) {

try{

// list of all client requirements
clientreq=[select id,name,RecordTypeid,Ownerid  from Client_Requirement__c];
system.debug('List of client requirement'+clientreq);
for(Client_Requirement__c cr: clientreq)
{
CRID.add(cr.id);// fetching the list of all client req IDS
}
// query for fetching the tasks that are not closed with in due date

listtask=[Select t.CreatedDate, t.Id, t.LastModifiedById, t.LastModifiedDate, t.OwnerId, t.Priority,
t.Status, t.Subject, t.WhatId, t.WhoId,t.ActivityDate,t.Due_Date__c from Task t where Whatid=:CRID and Status='Not Started'  and  ActivityDate=YESTERDAY];
system.debug('List of tasks that are not closed with in due date '+listtask);
 //d=listtask.ActivityDate;
if(listtask.size()>0)
{
for(task t: listtask)
{
if(t.OwnerId=='00590000001qNM7' || t.OwnerId=='00590000001qNMq' || t.OwnerId=='00590000001qNMM')
{
t.OwnerId='00590000001qNKB';// Assigning the crm owner to the all the tasks
listtask2.add(t);
}
}

}
update listtask2; //updating the tasks
system.debug('List of tasks that are updated to user crm head '+listtask2);

//--------------------------------------------------------------------------------------------------
clientreq1=[select id,name,RecordTypeid,Ownerid  from Client_Requirement__c where OwnerId='00590000001qNKB'];
system.debug('List of client requirement'+clientreq1);
for(Client_Requirement__c cr: clientreq1)
{
CRMID.add(cr.id);// fetching the list of all client req IDS
}

crmtasks1=[Select t.CreatedDate, t.Id, t.LastModifiedById, t.LastModifiedDate, t.OwnerId, t.Priority,
t.Status, t.Subject, t.WhatId, t.WhoId from Task t where Whatid=:CRMID and Status='Not Started' and ActivityDate=YESTERDAY];
system.debug('List of tasks that are not closed with in 1 days of crm user created records'+crmtasks1);
if(crmtasks1.size()>0)
{
for(task t: crmtasks1)
{
if(t.OwnerId=='00590000001qNKB')
{
t.OwnerId='00590000001WzWy';
crmtasks.add(t);
}
}
}
update crmtasks;
system.debug('List of crm tasks that are updated to system admin '+crmtasks);

//-----------------------------------------------------

clientreq2=[select id,name,RecordTypeid,Ownerid  from Client_Requirement__c];
system.debug('List of client requirement'+clientreq2);
for(Client_Requirement__c cr: clientreq2)
{
sysid.add(cr.id);// fetching the list of all client req IDS
}
system.debug('list of idssss for system admin'+sysid);

// query for fetching the task that crm head user not completed the tasks
listtask3=[Select t.CreatedDate, t.Id, t.LastModifiedById, t.LastModifiedDate, t.OwnerId, t.Priority,
t.Status, t.Subject, t.WhatId, t.WhoId from Task t where whatid=:sysid and Status='Not Started' and Due_Date_3_days__c=today];

system.debug('List of tasks that are not closed after 3 days '+listtask3);

if(listtask3.size()>0)
{
for(task t: listtask3)
{
if(t.OwnerId=='00590000001qNKB' || t.OwnerId=='00590000001qNM7' || t.OwnerId=='00590000001qNMq' )
{
t.OwnerId='00590000001WzWy';// Assigning the system admin to the tasks that crm head user not completed
listtask4.add(t);
}

}
update listtask4;  // updating the tasks.
system.debug('List of tasks that are updated to user sysadmin head '+listtask4);
}
}catch(Exception e){system.debug('@@@@@@@@@@@@@@'+e);}
}
}

 

Regards

venkatesh

SaraagSaraag

The list you're updating might be having duplicate elements. Make listtask2, listtask4, crmtasks into sets.  Before updating convert them into temp lists using addall() and then update those temp lists.

 

Edit: I haven't read the whole code, but use caution using sets, if you're updating a record multiple times make sure you have the update you really want to commit.

 

Saraag

Naveen NelavelliNaveen Nelavelli

List already having record with above Id ie,may be you didnt clear list properly after update is done.its best practice to intialize all methods either in constructor or separate method .use finally block to clear all the list..

can you post full error message

 

 

venkateshyadav1243venkateshyadav1243


hi

thanks for giving the replay

this is the error am getting

System.ListException: Duplicate id in list: 00T9000000O8CnMEAV

 

 

regards

venkatesh

venkateshyadav1243venkateshyadav1243

Hi saraag

 

Do you mean  i need to change like this

 

if i change like this it will work for me?

 

public set<task> listtask2=new set<task>();
public list<task> temp=new list<task>();
for(task t: listtask)
{
if(t.OwnerId=='00590000001qNM7' || t.OwnerId=='00590000001qNL6')
{
t.OwnerId='00590000001qNKB';

listtask2.add(t);
temp.addall(listtask2);
}
}
update temp;

 

 

Regards

venkatesh