How to Fix ContentDocumentLink Duplicate Errors?

When uploading files using <lightning-file-upload> in an LWC, Salesforce automatically creates a ContentDocumentLink between the uploaded file and the record-id provided. In your case, you are also creating another ContentDocumentLink in Apex to the same contact. This creates a conflict and causes the error:
Insert failed. First exception on row 0; first error: DUPLICATE_VALUE,
Document with ID ... is already linked with the entity ...: [LinkedEntityId]
So the main issue is that the file is already linked to the contact, and your Apex method tries to link it again, causing a duplicate ContentDocumentLink.
Below is the full explanation and the possible fixes.
Why the Error Happens
When using:
<lightning-file-upload
record-id={contactId}
onuploadfinished={handleUploadFinished}>
</lightning-file-upload>
Salesforce automatically:
- Creates a ContentVersion
- Creates a ContentDocument
- Creates a ContentDocumentLink linking the Document to
record-id
So if you upload the file to a Contact, a link already exists.
Your Apex code then runs:
links.add(new ContentDocumentLink(
ContentDocumentId = fileId,
LinkedEntityId = contactId
));
This creates a duplicate link, triggering the DUPLICATE_VALUE DML exception.
Multiple Ways to Fix the Issue
Fix Option 1 (Recommended): Do NOT manually create the link
Since Salesforce already links the file to the Contact automatically, remove the Apex insert.
Your updated logic:
LWC
Keep the component as:
<lightning-file-upload record-id={contactId}></lightning-file-upload>
Remove Apex code for linking
Simply remove saveFileAndAssociateWithContact entirely.
There is no need to manually create any ContentDocumentLink.
This is the simplest and correct solution.
Fix Option 2: Check if a link exists before inserting
If you still want to keep Apex logic (for auditing or additional processing), modify the Apex to avoid inserting duplicates:
Updated Apex
@AuraEnabled
public static void saveFileAndAssociateWithContact(Id contactId, List<Id> fileIds) {
List<ContentDocumentLink> existingLinks = [
SELECT ContentDocumentId, LinkedEntityId
FROM ContentDocumentLink
WHERE LinkedEntityId = :contactId
AND ContentDocumentId IN :fileIds
];
Set<String> existingPairs = new Set<String>();
for (ContentDocumentLink link : existingLinks) {
existingPairs.add(link.ContentDocumentId + '-' + link.LinkedEntityId);
}
List<ContentDocumentLink> inserts = new List<ContentDocumentLink>();
for (Id fileId : fileIds) {
String key = fileId + '-' + contactId;
if (!existingPairs.contains(key)) {
inserts.add(new ContentDocumentLink(
ContentDocumentId = fileId,
LinkedEntityId = contactId,
ShareType = 'I',
Visibility = 'AllUsers'
));
}
}
if (!inserts.isEmpty()) {
insert inserts;
}
}
This prevents duplicate linking.
Fix Option 3: Change LWC so Salesforce does NOT auto-link
If you want full manual control, set:
<lightning-file-upload record-id={null}></lightning-file-upload>
Now no ContentDocumentLink is created automatically, and your Apex handles everything.
However, this is rarely needed.
Fix Option 4: Use allow-multiple="false"
Sometimes duplicates happen when the Experience Cloud user uploads the same file twice very quickly.
Limiting multiple selection can reduce duplicate insert attempts, although it’s only a partial fix.
Final Recommendation
The best and simplest fix is:
Let Salesforce handle the linking automatically and stop creating links manually.
So delete or disable this part of your Apex logic:
insert links;
That completely resolves the issue.
Enroll for Career-Building Salesforce Training with 100% Money Back Guarantee
Our Salesforce course is carefully designed to give you a thorough understanding of the Salesforce platform, equipping you with the essential skills to succeed in the CRM industry. The program covers core modules like Salesforce Admin, Developer, and AI, combining theoretical concepts with hands-on practice. By engaging in real-world projects and interactive assignments, you’ll develop the expertise to solve business challenges using Salesforce solutions. Our expert instructors ensure you gain both technical proficiency and industry-relevant knowledge to excel in the Salesforce ecosystem.
In addition to technical training, our Salesforce Training in Dallas provides personalized mentorship, certification support, and interview preparation to enhance your career prospects. You’ll get access to extensive study resources, hands-on project experience, and dedicated guidance throughout the course. By completion, you’ll be well-prepared for certification exams and possess the problem-solving skills that employers look for. Begin your Salesforce journey today and unlock exciting career opportunities. Sign up for a Free Demo now!

