function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jodi Mahoney 18Jodi 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?
 
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!
Best Answer chosen by Jodi Mahoney 18
Amit Chaudhary 8Amit Chaudhary 8
Hi Jodi Mahoney 18 ,

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.
 
Task lastTask = tasks[tasks.size() - 1]; // Only one task it will take care
    for(Lead l : LeadMap.Values())
    {
        l.Last_Activity_Type__c = lastTask.Type;
    }

    // Update the Leads
    update LeadMap.values();
Please try below code and let us know if that will help you
trigger LastActivityType on Task (after insert,before update) 
{
 
	Set<Id> LeadIds = new Set<Id>();

	for(Task t :trigger.new)
	{
		if(t.whoId!=null)
		{
			Schema.SObjectType tType= t.whoId.getSObjectType();
			if(tType == Lead.Schema.SObjectType)
			{
				LeadIds.add(t.WhoId);
			}
		}
	}

	if(LeadIds.size() > 0 )	
	{
		Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Type__c from Lead where id in:LeadIds]);

		List<Lead> LeadList = new List<Lead>();
		Set<Id> setLeadIdinList = new Set<Id>();	
		for(Task t :Trigger.new)
		{
			if(t.whoId!=null && LeadMap.containsKey(t.whoId) )
			{
				if(setLeadIdinList.contains(t.whoId)== false)
				{
					Lead led = LeadMap.get(t.whoId);
					led.Last_Activity_Type__c = t.Type;
					LeadList.add(led);
					setLeadIdinList.add(t.whoId);
				}
			}
		}
		
		if(LeadList.size()>0)
		{
			update LeadList;
		}

	}
}


 

All Answers

SotosSotos
Hi,

you don't have to add the Leads in a list to update them.

You can simply do:
for( Lead l : LeadMap.values())
{
    l.last_Activity_Type__c = t.Type;
}
and then
update LeadMap.values();

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!
 
Jodi Mahoney 18Jodi Mahoney 18
Sotos- 

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?
 
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;
		}
	}
	// updating the Lead
	if(LeadMap.size()>0)
    {  
	update LeadMap.values();
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
trigger LastActivityType on Task (after insert,before update) 
{
 
	Set<Id> LeadIds = new Set<Id>();

	for(Task t :trigger.new)
	{
		if(t.whoId!=null)
		{
			Schema.SObjectType tType= t.whoId.getSObjectType();
			if(tType == Lead.Schema.SObjectType)
			{
				LeadIds.add(t.WhoId);
			}
		}
	}

	if(LeadIds.size() > 0 )	
	{
		Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Type__c from Lead where id in:LeadIds]);

		List<Lead> LeadList = new List<Lead>();
		Set<Id> setLeadIdinList = new Set<Id>();	
		for(Task t :Trigger.new)
		{
			if(t.whoId!=null && LeadMap.containsKey(t.whoId) )
			{
				if(setLeadIdinList.contains(t.whoId)== false)
				{
					Lead led = LeadMap.get(t.whoId);
					led.Last_Activity_Type__c = t.Type;
					LeadList.add(led);
					setLeadIdinList.add(t.whoId);
				}
			}
		}
		
		if(LeadList.size()>0)
		{
			update LeadList;
		}

	}
}
Let us know if this will help you
 
SotosSotos
Hi Jody,

I have updated the code and re arranged it a bit.
Compiles now :)
trigger LastActivityType on Task (after insert, before update) {
 
    Set<Id> LeadIds = new Set<Id>();

    List<Task> tasks = trigger.new;

    for(Task t : tasks)
    {
        if(t.whoId!=null && t.whoId.getSObjectType() == 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]);

    // No point to do anything else if there are no Leeds
    if(LeadMap.isEmpty())
        return;
    
    Task lastTask = tasks[tasks.size() - 1];
    for(Lead l : LeadMap.Values())
    {
        l.Last_Activity_Type__c = lastTask.Type;
    }

    // Update the Leads
    update LeadMap.values();
}

Let me know if it works :)
 
Jodi Mahoney 18Jodi Mahoney 18
Thanks Sotos it is compiling now.  I just put it in production, I want to run it for a bit and see if I still get the dup errors.  I'll let you know if it works!  Thank you
Amit Chaudhary 8Amit Chaudhary 8
Hi Jodi Mahoney 18 ,

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.
 
Task lastTask = tasks[tasks.size() - 1]; // Only one task it will take care
    for(Lead l : LeadMap.Values())
    {
        l.Last_Activity_Type__c = lastTask.Type;
    }

    // Update the Leads
    update LeadMap.values();
Please try below code and let us know if that will help you
trigger LastActivityType on Task (after insert,before update) 
{
 
	Set<Id> LeadIds = new Set<Id>();

	for(Task t :trigger.new)
	{
		if(t.whoId!=null)
		{
			Schema.SObjectType tType= t.whoId.getSObjectType();
			if(tType == Lead.Schema.SObjectType)
			{
				LeadIds.add(t.WhoId);
			}
		}
	}

	if(LeadIds.size() > 0 )	
	{
		Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Type__c from Lead where id in:LeadIds]);

		List<Lead> LeadList = new List<Lead>();
		Set<Id> setLeadIdinList = new Set<Id>();	
		for(Task t :Trigger.new)
		{
			if(t.whoId!=null && LeadMap.containsKey(t.whoId) )
			{
				if(setLeadIdinList.contains(t.whoId)== false)
				{
					Lead led = LeadMap.get(t.whoId);
					led.Last_Activity_Type__c = t.Type;
					LeadList.add(led);
					setLeadIdinList.add(t.whoId);
				}
			}
		}
		
		if(LeadList.size()>0)
		{
			update LeadList;
		}

	}
}


 
This was selected as the best answer
Jodi Mahoney 18Jodi Mahoney 18
This worked thanks!!