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
Nathan Prats 22Nathan Prats 22 

Test Class on ContentDocumentLink Trigger

Hello, 

I created this trigger which set a share type for each new content document link. 
trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
    
    for(ContentDocumentLink cdl: Trigger.new){
        cdl.shareType = 'I';
    } 
}
I tried to create this test class:
@isTest
public class ContentDocumentLinkTriggerTest {
    
    @isTest static void ContentDocumentLinkTriggerTest() {
        
        // Create a ContentVersion
        ContentVersion ContentDoc = new ContentVersion();
        ContentDoc.Title = 'My Doc';
        ContentDoc.ContentUrl= 'test.com';
        Insert ContentDoc;
        
        // Create a ContentDocumentLink
        ContentDocumentLink ContentDL = new ContentDocumentLink();
        ContentDL.ContentDocumentId = ContentDoc.Id;
        Insert ContentDL;
        
        // Verify the share type is = i
        System.assertEquals(ContentDL.ShareType,'I');
        
        
    }
}

But I receive this error: 
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, ContentDocument ID: id value of incorrect type: 0683E0000003EXVQA2: [ContentDocumentId]

Any idea why ? 
Best Answer chosen by Nathan Prats 22
Amit Chaudhary 8Amit Chaudhary 8
Please check below post i hope that will help you

1) https://developer.salesforce.com/forums/?id=906F0000000AbR9IAK

Update your code like below
@isTest
public class ContentDocumentLinkTriggerTest {
    
    @isTest static void ContentDocumentLinkTriggerTest() {
        
        // Create a ContentVersion
        ContentVersion ContentDoc = new ContentVersion();
        ContentDoc.Title = 'My Doc';
        ContentDoc.ContentUrl= 'test.com';
        Insert ContentDoc;
        
		ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
		
        // Create a ContentDocumentLink
        ContentDocumentLink ContentDL = new ContentDocumentLink();
        ContentDL.ContentDocumentId = testcontent.contentdocumentid;
        Insert ContentDL;
        
        // Verify the share type is = i
        // Please query the record before assert
         //System.assertEquals(ContentDL.ShareType,'I');
        
        
    }
}
Let us know if this will help you
 

All Answers

Sampath SuranjiSampath Suranji
Amit Chaudhary 8Amit Chaudhary 8
Please check below post i hope that will help you

1) https://developer.salesforce.com/forums/?id=906F0000000AbR9IAK

Update your code like below
@isTest
public class ContentDocumentLinkTriggerTest {
    
    @isTest static void ContentDocumentLinkTriggerTest() {
        
        // Create a ContentVersion
        ContentVersion ContentDoc = new ContentVersion();
        ContentDoc.Title = 'My Doc';
        ContentDoc.ContentUrl= 'test.com';
        Insert ContentDoc;
        
		ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
		
        // Create a ContentDocumentLink
        ContentDocumentLink ContentDL = new ContentDocumentLink();
        ContentDL.ContentDocumentId = testcontent.contentdocumentid;
        Insert ContentDL;
        
        // Verify the share type is = i
        // Please query the record before assert
         //System.assertEquals(ContentDL.ShareType,'I');
        
        
    }
}
Let us know if this will help you
 
This was selected as the best answer
Nathan Prats 22Nathan Prats 22
Thanks !!

It passed like this : 
 
@isTest
public class ContentDocumentLinkTriggerTest {
    
    private static String name='Test Account';
    
    @isTest static void ContentDocumentLinkTriggerTest() {
        
        // Create an account with an opportunity
        Account acct = TestUtils.CreateAccount(name);
        Account pacct = TestUtils.CreatePartnerAccount(name+' partner');
        Contact con  = TestUtils.CreateContact('firstName','lastName',acct);
        Opportunity opp = TestUtils.CreateOpportunity(acct, pacct, con);
        
        // Create a ContentVersion
        ContentVersion ContentVer = new ContentVersion();
        ContentVer.Title = 'My Doc';
        ContentVer.ContentUrl= 'test.com';
        Insert ContentVer;
        
        // Create a ContentDocumentLink
        ContentDocumentLink ContentDL = new ContentDocumentLink();
        ContentDL.ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: ContentVer.Id].ContentDocumentId;
        ContentDL.LinkedEntityId=opp.id;
        ContentDL.ShareType='V';
        Insert ContentDL;
        
        // Retrieve ? 
        ContentDL = [SELECT Id,ShareType FROM ContentDocumentLink WHERE Id =:ContentDL.Id];
        System.debug('Troll ' + ContentDL.ShareType);
        
        
        // Verify the share type is = i
        System.assertEquals('I',ContentDL.ShareType);
        
        
    }
}