function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jan Kopejtko 2Jan Kopejtko 2 

Test class for a simple trigger on ContentDocumentLink

I have a simple trigger on ContentDocumentLink. The trigger finds the linked record's field and stores it's value in a ContentVersion field Title.

So we have four objects that are linked like this: ContentVersion -> ContentDocument -> ContentDocumentLink -> Some object

I take field from Some object and populate ContentVersion Title with the value. There is the code:
 
trigger ContentDocumentLink on ContentDocumentLink (before insert) {
    Set<Id> aids = new Set<Id>();
    Set<Id> bids = new Set<Id>();
    Set<Id> cids = new Set<Id>();
    Set<Id> dids = new Set<Id>();
    
    for(ContentDocumentLink a :Trigger.New) {
        aids.add(a.Id);
        bids.add(a.ContentDocumentId);
        cids.add(a.LinkedEntityId);
        
    }
	ContentDocument[] b = [Select Id, LatestPublishedVersionId, Title from ContentDocument Where Id in :bids];
    
    for(ContentDocument c :b) {
        dids.add(c.LatestPublishedVersionId);
     
    ContentVersion[] d = [Select Id, Title from ContentVersion where Id in :dids];
        for(ContentVersion e :d) {
            e.Title = 'letitgo';
            update e;
        }
        
    }
    

}

Question is how do I write the test class? I have 4 linked objects I need to use. Which one do I insert first?
 
PRAKASH JADA 13PRAKASH JADA 13
Hi,

By the below approach you can create test data for once and you can use it for all methods. In this way, the execution speed can be controlled for test class while deploying the code to production.



@isTest
public class ContentDocumentLinkTest {
  @TestSetup static void prepareTestData() {
    // Create a test data for ContentVersion
    ContentVersion testContentVer = new ContentVersion();
    // Field assignment will go here

    insert testContentVer;
    
    // Create a test data for ContentDocument
    ContentDocument testContentDoc = new ContentDocument();
    // Field assignment will go here
    testContentDoc.LatestPublishedVersionId = testContentVer.Id; // Main field
    inset testContentDoc;


    
    // Create a test data for ContentDocumentLink
    ContentDocumentLink testDocLink = new ContentDocumentLink();
    // Field assignment will go here
    
    testDocLink.ContentDocumentId = testContentDoc.Id; // Main value
    insert testDocLink;

    //Some system asserts to check for the correct test data
    System.assert(testContentVer != null);
    System.assertEquals(testContentVer.Id, testContentDoc.LatestPublishedVersionId);
    System.assertEquals(testContentDoc.Id, testDocLink.ContentDocumentId);
  
  }

  // Actual test method
  @isTest static void testMethod1() {
    ContentVersion testContentVersion = [SELECT ID, Title from ContentVersion LIMIT 1];
    testContentVersion.Title = 'letitgo';
    update testContentVersion; 
  }
}
Jan Kopejtko 2Jan Kopejtko 2
Hello, this is my code now:
 
trigger UpdateFileTitle on ContentDocumentLink (before insert) {
    Set<Id> cdids = new Set<Id>();
    Set<Id> cvids = new Set<Id>();
    Set<Id> recordid = new Set<Id>();
    
    for(ContentDocumentLink cdl :Trigger.New) {
        cdids.add(cdl.ContentDocumentId);
        recordid.add(cdl.LinkedEntityId);
        
    }
    ServiceAppointment[] sa = [Select Id, FileTitle__c from ServiceAppointment where Id in :recordid];
    ContentDocument[] cd = [Select Id, LatestPublishedVersionId, Title from ContentDocument Where Id in :cdids];
    for(ContentDocument a :cd) {
        cvids.add(a.LatestPublishedVersionId);
    }
    ContentVersion[] cv = [Select Id, Title from ContentVersion where Id in :cvids];
    for(ContentDocumentLink cdl2 :Trigger.new){
        for(ContentVersion b :cv){
            for(ServiceAppointment c :sa){
                b.Title = c.FileTitle__c;
                update b;
            }
            
        }
    }

}
The trigger works fine now, I tested it all manually, it' really great.

I'm writing the test class the way you mentioned. I found out that it is impossible to insert a ContentDocument record, becuase it is craeted automaticly with content version. My test class now looks like this:
 
@isTest
public class UpdateFileTitleTest {
    @TestSetup static void prepareTestData() {
    ContentVersion testContentVer = new ContentVersion();
        testContentVer.Title = 'test';
        testContentVer.PathOnClient = 'file.jpg';
        testContentVer.VersionData = Blob.valueOf('test');
        insert testContentVer;
        
        WorkOrder wo = new WorkOrder();
        insert wo;
        ServiceAppointment sa = New ServiceAppointment();
        sa.Status = 'New';
        sa.ParentRecordId = wo.Id;
        sa.FileTitle__c = 'test';
        insert sa;
        
        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.ContentDocumentId = testContentVer.ContentDocumentId;
        cdl.LinkedEntityId = sa.id;
        insert cdl;

              
            System.assert(testContentVer != null);
    System.assertEquals(testContentVer.Id, cdl.ContentDocumentId);
        
        
    }
    
    @isTest public static void testMethod1(){
        ContentVersion testContentVersion = [Select id, Title from ContentVersion LIMIT 1];
        System.assertEquals('test', testContentVersion.Title);
    }
    

}

I got an error:
Internal Salesforce Error: 78176448-104125 (-759335939) (-759335939)

That is a big problem, help me with this please, how do I fix this?
Jan Kopejtko 2Jan Kopejtko 2
I already found the solution, thanks alot Prakash