0

We are transitioning our org to LEX and there is code that has to be updated from Attachments to Files...i.e. ContentVersion, ContentDocument and ContentDocumentLink. One of the updates is a trigger that was on Attachments that ran on beforeInsert, beforeUpdate and beforeDelete that would use the addError method to display a custom message to prevent Attachments from being inserted, updated or deleted when certain conditions where met during an approval process.

Based on some documentation I found, I created a new trigger on the ContentDocumentLink which has the LinkedEntityId reference field to the parent object that the attachment is related to. There appears to be a known issue that the custom error message does not display in LEX and instead displays a standard error message with Unable to Upload Files, which is okay in this scenario. The custom message isn't mandatory. I've tested the code and it does appear to work for insert, update and delete. However, I'm working on the unit tests and I get coverage for the insert, but cannot get the code to fire for edit and delete.

Is it possible to cover an update and delete on the ContentDocumentLink trigger? What record do I need to update and delete to fire the trigger on the ContentDocumentLink in the unit test?

1
  • Are you trying to update/delete the same record that you are inserting in the test class? Can you add some sample code that you have tried. Also, there is another consideration that you might need to take a look on, that in LEX trigger doesn't fire on ContentDocumentLink. Refer the article here – Hemant Jain Jul 18 '20 at 15:21
0

You can create a ContentDocumentLink record in test class and work with it to cover insert, update, and delete scenarios. Below is a sample code:

@isTest 
static void contentDocLinkTriggerTest(){
    
    Account acc = new Account(Name = 'Test Account');
    insert acc;
    
    ContentVersion contentVer = new ContentVersion(Title = 'Test Account Document',
                                          PathOnClient = 'Test.jpg',
                                          VersionData = Blob.valueOf('Test Account Document'));
    insert contentVer;
    
    ContentDocument contentDoc = [SELECT Id FROM ContentDocument WHERE Title = 'Test Account Document' Limit 1];
    
    ContentDocumentLink contentDocLink = new ContentDocumentLink();
    contentDocLink.ContentDocumentId = contentDoc.Id;
    contentDocLink.LinkedEntityId= acc.Id;
    contentDocLink.ShareType='V';
    contentDocLink.Visibility='AllUsers';
    insert contentDocLink;
    
    contentDocLink.ShareType = 'I';
    update contentDocLink;
    
    delete contentDocLink;
}
1
  • Thank you @Prakhar Saxena – Dman100 Jul 20 '20 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.