0

I have a custom lwc component to upload files to a record. I used Lightning file upload for this. And after files are uploaded I show them in the form of a datatable with a download button for every file download. I am also having a delete button for every file to delete. I have written the logic but I am getting an error field integrity exception when trying to delete them.

deleteFile(fileId) {
        deleteFile({ fileId })
            .then(() => {
                this.files = this.files.filter(file => file.id !== fileId);
                this.dispatchEvent(new ShowToastEvent({
                    title: 'Success',
                    message: 'File deleted successfully',
                    variant: 'success'
                }));
            })
            .catch(error => {
                this.dispatchEvent(new ShowToastEvent({
                    title: 'Error deleting file',
                    message: error.body.message,
                    variant: 'error'
                }));
            });
    }
}

There is an apex controller to this too.

@AuraEnabled()
    public static void deleteFile(String contentDocumentId) {
        if (!hasDeletePermission()) {
            throw new AuraHandledException('You do not have permission to delete this file.');
        }
        
        System.debug('Deleting ContentDocumentLink with ID: ' + contentDocumentId);  // Debug log
        
        ContentDocumentLink[] filesToDelete = [SELECT Id FROM ContentDocumentLink WHERE ContentDocumentId = :contentDocumentId];
        // Check if there are any links to delete
        if (!filesToDelete.isEmpty()) {
        // Delete the ContentDocumentLink
        delete filesToDelete;
       }

       List<ContentDocument> docToDelete = [SELECT Id FROM ContentDocument WHERE Id = :contentDocumentId];
       if(!docToDelete.isEmpty()){
        delete docToDelete; // This will delete the actual file
       }else{
           throw new AuraHandledException('No ContentDocumentLink found for the given ID.');
       } 
    }
   
   private static Boolean hasDeletePermission() {
    // Check if the user has delete permission on ContentDocument and ContentDocumentLink
    return Schema.sObjectType.ContentDocument.isDeletable() && Schema.sObjectType.ContentDocumentLink.isDeletable();
   }
}

Is there anything mistake in my code? I think the logic is fine. But I am getting this error field integrity exception while trying to delete them. Can anyone help me with this?

5
  • 1
    Welcome to SFSE! Please take the tour and read How to Ask. Is the deletion happening directly from the LWC, or do you have an Apex controller that is doing the DML operation, and if so could you include the code for that? Could you please edit your question to add more details. Commented Sep 12 at 7:56
  • I have modified and added the apex controller too. Can you check now and see if there is any error in the apex code
    – Vijay Babu
    Commented Sep 12 at 9:15
  • I have got it right deleting from ContentDocumentLink is not possible. Directly deleting From ContentDocument is correct. Removing the query from ContentDocumentLink and the next dml delete statement will ensure the code works.
    – Vijay Babu
    Commented Sep 12 at 10:37
  • Adding for posterity. Here is ContentDocumentLink help and it doesn't list delete() as allowed operation developer.salesforce.com/docs/atlas.en-us.object_reference.meta/…
    – datasert
    Commented Sep 12 at 15:29
  • @VijayBabu You may want to submit your finding as anwer and accept that.
    – datasert
    Commented Sep 12 at 15:30

0

You must log in to answer this question.