4

I received this error when converting leads into contacts.

Apex script unhandled trigger exception by user/organization . AccountActivity: execution of AfterUpdate caused by: System.ListException: Duplicate id in list:

Tried to modify but unable to do .Anything recommended

List<Account> account = new List<Account>();
for(Task t : Trigger.new)
{

  if(t.Whatid!=null && ((string)t.Whatid).startsWith('001')) //Check if the Task is not null and is on an Account 
  {
    Account acc = [Select Last_Contact__c, LastActivityDate from Account where id =: t.whatId ];
    if (acc != null && (t.Type=='Cold Call' || t.Type=='Warm Call' || t.Type=='Client Call' || t.Type=='Meeting' || t.Type=='Client Meeting')) //Check if the Task type is not Email and other 
    {

      acc.Last_Contact__c = acc.LastActivityDate; //Update the custom field 
      account.add(acc);
    }

  }
}
update account; //update the account 

update account gives error

2 Answers 2

3

Your lead has multiple tasks that are being converted to the account, which causes the account to appear more than once in the list.

You'll want to create a map so you can update each account just once. Your trigger needs additional help, such as the query inside the loop.

I'm on mobile right now, but let me get you started with some pseudocode:

Make a list of all tasks that meet the criteria for updating accounts.

Query all those accounts and put them in a map.

For each task in the list of tasks that need to update accounts, update the account in the map.

Update the account map's values.

I'll write some more formal code later.

2

Since sfdcfox didn't get to it, here is some sample code to get you over the hump

Set<Id> accountIds = new Set<Id>();
for(Task t : Trigger.new) {
  if (t.Whatid != null && 
     ((string)t.Whatid).startsWith('001') && 
     (t.Type=='Cold Call' || t.Type=='Warm Call' || t.Type=='Client Call' ||
      t.Type=='Meeting' || t.Type=='Client Meeting')) {

     accountIds.add(t.whatId); 
  }
}

List<Account> accounts = [Select Last_Contact__c, LastActivityDate from Account where id in :accountIds ]; 

for (Account acc : accounts] {
  acc.Last_Contact__c = acc.LastActivityDate; //Update the custom field 
  account.add(acc);
}

update accounts; //update the accounts

For the record, it is really bad practice to put SELECT statements inside of for-loops. In triggers this is especially dangerous as such code will fail when inserting tasks in bulk.

2
  • 1
    Minor suggestion: (1) t.whatId.getSObjectType().getDescribe().getName() == 'Account' or t.whatId == t.accountId to get Tasks associated solely with Accounts in lieu of ((string)t.whatId).startsWith('001') ... avoids the implicit knowledge that AccountIds start with 001 - a pedantic point, to be sure :-)
    – cropredy
    Commented Dec 25, 2015 at 7:19
  • Alternatively, if(t.whatid != null && t.whatid.getsobjecttype() == account.sobjecttype... which means no describe required...
    – sfdcfox
    Commented Dec 25, 2015 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .