+ Start a Discussion
venkateshyadav1243venkateshyadav1243 

System.ListException: Duplicate id in list

Hi
Am getting the this error "System.ListException: Duplicate id in list"
can any one please tell me,how to avoid this error
below is the my code.

global with sharing class Task_Esc_duedate_users_to_crm_user implements Schedulable
{
public List<Client_Requirement__c> clientreq=new list<Client_Requirement__c>();
public list<task> listtask=new List<task>();
public list<task> listtask2=new List<task>();
public set<id> CRID=new set<id>();
public list<user> crmusernewid=new list<user>();


global void execute(SchedulableContext SC) {
try
{
map<Id, User> Id2User = new map<Id, User>([SELECT Id, Name FROM User WHERE Profile.Name IN ('East','Howrah','North and Central','Rajarhat','South I','South II','Newtown')]);
crmusernewid =[SELECT Id, Name FROM User WHERE Active_user__c=false and Profile.Name IN ('System Admin')];

clientreq=[select id,name,RecordTypeid,Ownerid  from Client_Requirement__c];
system.debug('List of all  client requirement'+clientreq);
for(Client_Requirement__c cr: clientreq)
{
CRID.add(cr.id);// fetching the list of all client req IDS
}
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 AND OwnerID IN :ID2User.keyset()];
system.debug('List of tasks that are not closed with in due date of all users '+listtask);
for(task t: listtask)
{
for(user crm:crmusernewid)
{
t.OwnerId=crm.id;// 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);
}
catch(Exception e){system.debug('@@@@@@@@@@@@@@'+e);}
}
}

Functionality is assiging the all users task to crm head user.after one day.

Regards
venkatesh
Best Answer chosen by venkateshyadav1243
Vinit_KumarVinit_Kumar
Agreed with Sherman try using map something like below ..

Map<Id,Task> taskMap = new Map<Id,Task>();

then instead of list try using map to insert the values and then update them..

change the below code from

listtask2.add(t);

to

taskMap.put(t.id,t);

and then 

update taskMap.values();


All Answers

harsha__charsha__c
Hi Venkat,

The Tasks are multiple times into the list(same task with different crm Ids) and there by the update DML thrown the duplicate Ids issue.

Replace the below part with the below next block

************************************************************************* 
for(task t: listtask)
{
    for(user crm:crmusernewid)
    {
        t.OwnerId=crm.id;// Assigning the crm owner to the all the tasks
        listtask2.add(t);
    }
}
**************************************************************************

--------------------------------------------------------------------------------------------
Set<Task> setTasksToUpdate = new Set<Task>();
listtask2 = new List<Task>();
for(task t: listtask)
{
    for(user crm:crmusernewid)
    {
        t.OwnerId=crm.id;// Assigning the crm owner to the all the tasks
        setTasksToUpdate.add(t);
    }
}

listtask2.addAll(setTasksToUpdate);
----------------------------------------------------------------------------------------------
// update the list now

Regards,
- Harsha
Norm Sherman SFNorm Sherman SF
Hmm instead of using a List I would use a Map, i.e. Map<Id,Task), that way you are guaranteed to not have duplicate ids. Then just do update MyMapName.Values()
venkateshyadav1243venkateshyadav1243
hi sherman

can please provide some more information
am not clear what you are saying.


regards
venkatesh.
Vinit_KumarVinit_Kumar
Agreed with Sherman try using map something like below ..

Map<Id,Task> taskMap = new Map<Id,Task>();

then instead of list try using map to insert the values and then update them..

change the below code from

listtask2.add(t);

to

taskMap.put(t.id,t);

and then 

update taskMap.values();


This was selected as the best answer
venkateshyadav1243venkateshyadav1243
Hi am updating the task using map,
am facing one issue when i run schedular class am getting problem.

exapmle: today i run my schedular class its updated 10 records,
next day i run ischedular i have 5 records to update,
but in 2 day its updating 15 records

its fecthing old records and updating every how to clear after update?