In Salesforce Apex, a List can store duplicate values, including duplicate sObject IDs. However, if you attempt to perform a DML (Data Manipulation Language) operation — such as update or delete — on a List that contains sObjects with duplicate IDs, the platform throws the error: 'System.ListException: Duplicate id in list'.
This error most commonly occurs when programmatically constructing a list of records to update or delete and inadvertently adding the same record ID more than once. For example, a loop that queries and adds records to a list may add the same record under different conditions, resulting in duplicates.
To safely perform DML on a collection that may contain duplicate IDs, convert the List to a Map using the Map(Id, SObject) constructor or the putAll() method. A Map in Apex enforces unique keys — duplicate IDs are automatically overwritten with the last occurrence, leaving one entry per unique ID. After conversion, call DML operations on mapName.values() to process only unique records.
Map<Id, SObject> — for example, Map<Id, Account> accMap = new Map<Id, Account>().accMap.putAll(yourList) to populate the map from your list. Duplicate IDs are automatically deduplicated.accMap.values() — for example, update accMap.values().This is one recommended approach to resolve this error and is not the only solution. Review your code logic to understand how duplicate IDs are being introduced, and test your fix in a sandbox environment before deploying to production.
000382864

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.