Sunday 5 July 2015

How to deal with: System.ListException: Duplicate id in List [duplicate]


Sometimes you may have received this error:
System.ListException: Duplicate id in List [duplicate]..

The caveat is: List should not hold multiple instances of the same object. Otherwise you will get this run time exception.

It's better to user set or map as they remove the duplicates.

Account oAccount = new Account(Id = sSfdcAccountId) ;
list<Account> listAccountWithDuplicateIds = new list<Account>() ;
listAccountWithDuplicateIds.add(oAccount) ;
Database.insert(listAccountWithDuplicateIds, false) ;

will result in System.ListException: Duplicate id in List [duplicate]..

As per your needs, use either:

1. map<String, Account> mapAccountId2Account = new map<String, Account>() ;
mapAccountId2Account.add(oAccount .Id, oAccount ) ;
Database.insert(mapAccountId2Account.values(), false) ;

2. setAccount.addAll(listAccountWithDeuplicate) ;

#MySfdcTweets #SalesforceDotCom #SFDC

No comments:

Post a Comment

Thank you for visiting. Your comments are highly appreciated.