Search code examples
salesforceapexsalesforce-lightningsalesforce-service-cloudsalesforce-communities

How to test contentdocumentlink trigger for Salesforce Prod deployment


I am trying to deploy a trigger to prod on salesforce. I was hoping someone could help me with an example of tests for this trigger.

Here is my trigger. It does its purpose, which is to update a bool field when a new contentNote (or anything of content type) that then has collateral effects through process builder.

trigger NewNote on ContentDocumentLink (before insert) {
Set<Id> setParentId = new Set<Id>();
List<Client_Relationships__c> crlst = new List<Client_Relationships__c>();

for (ContentDocumentLink cdl : trigger.new ) {
    setParentId.add(cdl.LinkedEntityId);
    }
crlst = [select Id , newNote__c from Client_Relationships__c where Id IN :setParentId];
For(Client_Relationships__c e : crlst)
 {
    e.newNote__c = True;
 }
 update crlst;
} 

Solution

  • The trigger you wrote can be more efficient by omitting the SOQL query as seen below:

    trigger NewNote on ContentDocumentLink (before insert) {
       List<Client_Relationships__c> crlst = new List<Client_Relationships__c>();
    
       for (ContentDocumentLink cdl : trigger.new ) {
          if(cdl.LinkedEntityId.getSObjectType().getDescribe().getName() == 'Client_Relationships__c'){
             crlst.add(
                new Client_Relationships__c(
                   Id = cdl.LinkedEntityId,
                   newNote__c = true
                )
             );
          }
       }
       update crlst;
    } 
    

    The best practice would be to add your code to a handler or utility class and to only have one trigger per object. The name of this trigger could be changed to "ContentDocumentLinkTrigger" if you adopt that practice.

    The test class for that trigger is below. I could not test the compilation because I don't have the same custom object.

    @IsTest
    private class ContentDocumentLinkTriggerTest {
    
        @TestSetup
        static void setupTest() {
            insert new ContentVersion(
                    Title = 'Test_Document.txt',
                    VersionData = Blob.valueOf('This is my file body.'),
                    SharingPrivacy  = 'N',
                    SharingOption   = 'A',
                    Origin          = 'H',
                    PathOnClient    = '/Test_Document.txt'
            );
            List<Client_Relationships__c> relationships = new List<Client_Relationships__c>();
            for(Integer i = 0; i < 300; i++){
                relationships.add(
                        new Client_Relationships__c(
                                //add required field names and values
                        )
                );
            }
            insert relationships;
        }
    
        static testMethod void testInsertTrigger() {
            //prepare data
            List<ContentVersion> contentVersions = new List<ContentVersion>([
                    SELECT Id, ContentDocumentId FROM ContentVersion
            ]);
            System.assertNotEquals(0, contentVersions.size(), 'ContentVersion records should have been retrieved');
            List<Client_Relationships__c> relationships = getAllClientRelationships();
            System.assertNotEquals(0, relationships.size(), 'Client Relationship records should have been retrieved.');
            List<ContentDocumentLink> documentLinks = new List<ContentDocumentLink>();
            for(Integer i = 0; i < 252; i++){
                documentLinks.add(
                        new ContentDocumentLink(
                                ContentDocumentId = contentVersions[0].ContentDocumentId,
                                LinkedEntityId = relationships[i].Id,
                                ShareType = 'I'
                        )
                );
            }
            //test functionality
            Test.startTest();
                insert documentLinks;
            Test.stopTest();
    
            //assert expected results
            List<Client_Relationships__c> relationshipsAfterProcessing = getAllClientRelationships();
            for(Client_Relationships__c relationship : relationshipsAfterProcessing){
                System.assert(relationship.newNote__c, 'The newNote__c field value should be true.');
            }
        }
    
        private static List<Client_Relationships__c> getAllClientRelationships(){
            return new List<Client_Relationships__c>([
                    SELECT Id, newNote__c FROM Client_Relationship__c
            ]);
        }
    }
    

    For setting up test data, it is helpful to have a utility class that centralizes the creation of well-formed records. This is extremely useful when your code base gets large and a validation rule affects the insertion of new data in many test classes. With a centralized method, the inserted data only needs to be altered once.