+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

how to write test class for Content Document Link

Bwlow code of my class we we not cover content documt link area
public class BookingTriggerHandler{
    public static void AddNotes(List<Booking__c> lstbook){
        
        List<ContentDocumentLink> insertlst = New List<ContentDocumentLink>();
        Map<id,Booking__c> mapIdtoBooking = New Map<Id,Booking__c>();
        for(Booking__c objbook :lstbook){
            if(objbook.opportunity__c != null){
                mapIdtoBooking.put(objbook.opportunity__c, objbook);
            }
        }
        system.debug('----mapIdtoBooking----'+mapIdtoBooking);
       
        for(ContentDocumentLink objlink:[SELECT Id,LinkedEntityId ,contentDocumentId FROM ContentDocumentLink where LinkedEntityId IN: mapIdtoBooking.keySet()]){
    		ContentDocumentLink obj = New ContentDocumentLink();
    		obj = objlink.clone(false,true);
    		obj.LinkedEntityId = mapIdtoBooking.get(objlink.LinkedEntityId).id;
    		insertlst.add(obj);            
		}
        system.debug('----insertlst----'+insertlst);
        
        if(!insertlst.isEmpty()){
           insert insertlst; 
        }
    }
}
@isTest
public class BookingTriggerHandlerTest{
    static testMethod void Testdata(){
        List<ContentDocumentLink> lstcon = New List<ContentDocumentLink>();
        List<Booking__c> lstbooking = New List<Booking__c>();
        Account objacc = New Account();
        objacc.name = 'Test';
        objacc.Type = 'Press';
        insert objacc;
        
        Opportunity objopp = new Opportunity();
        objopp.name ='Test';
        objopp.StageName ='Closed Won';
        objopp.CloseDate = system.today();
        objopp.Mode_of_Contact__c = 'Telephone';
        objopp.AccountId = objacc.Id;
        insert objopp;
        
        Booking__c objbook = New Booking__c();
        objbook.Name = 'Test';
        objbook.Opportunity__c = objopp.Id;
        lstbooking.add(objbook);
        insert lstbooking;
        List<contentNote> lstnote = New List<contentNote>();
        contentNote objnote = New contentNote();
        objnote.title = 'Test';
        lstnote.add(objnote);
        insert lstnote;
        
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument where Id =:objopp.Id]; 
        if(!documents.isEmpty()){
           ContentDocumentLink contentlink=new ContentDocumentLink();
		   contentlink.ShareType= 'C';
		   contentlink.LinkedEntityId = objopp.Id; 
		   contentlink.ContentDocumentId=documents[0].Id;
		   contentlink.Visibility = 'AllUsers'; 
           lstcon.add(contentlink); 
           System.assertEquals(documents.size(), 1);
        }		
		insert lstcon;
    }
}
Best Answer chosen by Bablu Kumar Pandit
David Zhu 🔥David Zhu 🔥
My understanding of your code is you are trying to attach content note from parent Opportunity to Booking record.
then you need to create contont note before insert booking record.
You may use the following code snipet.
@isTest
public class BookingTriggerHandlerTest{
    static testMethod void Testdata(){
        List<ContentDocumentLink> lstcon = New List<ContentDocumentLink>();
        List<Booking__c> lstbooking = New List<Booking__c>();
        Account objacc = New Account();
        objacc.name = 'Test';
        objacc.Type = 'Press';
        insert objacc;

        Opportunity objopp = new Opportunity();
        objopp.name ='Test';
        objopp.StageName ='Closed Won';
        objopp.CloseDate = system.today();
        //objopp.Mode_of_Contact__c = 'Telephone';
        objopp.AccountId = objacc.Id;
        insert objopp;
        
        List<ContentVersion> lstnote = New List<ContentVersion>();
        ContentVersion objnote = New ContentVersion();
        objnote.title = 'Test';
        objnote.PathOnClient = objnote.Title + '.snote';
        objnote.VersionData = Blob.valueof('this is a test');
        objnote.FirstPublishLocationId = objopp.Id;
        
        lstnote.add(objnote);
        insert lstnote;

        Booking__c objbook = New Booking__c();
        objbook.Name = 'Test';
        objbook.Opportunity__c = objopp.Id;
        lstbooking.add(objbook);
        insert lstbooking;
        

        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument where Id =:objopp.Id];
        if(!documents.isEmpty()){
           ContentDocumentLink contentlink=new ContentDocumentLink();
		   contentlink.ShareType= 'C';
		   contentlink.LinkedEntityId = objopp.Id;
		   contentlink.ContentDocumentId=documents[0].Id;
		   contentlink.Visibility = 'AllUsers';
           lstcon.add(contentlink);
           System.assertEquals(documents.size(), 1);
        }
		insert lstcon;
    }
}


 

All Answers

Ricky Lowe 19Ricky Lowe 19
Hi Bablu,

To test the insert of a ContentDocumentLink you need to first insert a ContentVersion. See below for test class method to create ContentDocumentLink:
 
public static void createDocumentsAndLink(Id linkedTo, Integer noOfAttachments){
        //insert Content version
        list<ContentVersion> conVers = new list<ContentVersion>();
        for(integer i =0; i<noOfAttachments; i++){
            ContentVersion ContVers = new ContentVersion();
            ContVers.Title = 'Testing multiple att - ' + i;
            ContVers.PathOnClient = 'Carrots.jpg';
            ContVers.VersionData = Blob.valueOf('Carrots');
            ContVers.IsMajorVersion = true;
            conVers.add(ContVers);
        }
        insert conVers;

        map<Id, ContentVersion> contMap = new map<Id, ContentVersion>(conVers);
            

        //Query to get Doco
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument WHERE LatestPublishedVersionId IN :contMap.KeySet()];

            
        list<ContentDocumentLink> conDocLinks = new list<ContentDocumentLink>();
        for(integer i =0; i<noOfAttachments; i++){
            //insert content link against Service Appointment
            ContentDocumentLink ConLink = new ContentDocumentLink();
            ConLink.LinkedEntityId = linkedTo;
            ConLink.ContentDocumentId = documents[i].Id;
            ConLink.shareType = 'V';
            conDocLinks.add(ConLink);
        }
        insert conDocLinks;
    }
Cheers,

Ricky
 
Bablu Kumar PanditBablu Kumar Pandit
@isTest
public class BookingTriggerHandlerTest{
    static testMethod void Testdata(){
        List<ContentDocumentLink> lstcon = New List<ContentDocumentLink>();
        List<Booking__c> lstbooking = New List<Booking__c>();
        Account objacc = New Account();
        objacc.name = 'Test';
        objacc.Type = 'Press';
        insert objacc;
        
        Opportunity objopp = new Opportunity();
        objopp.name ='Test';
        objopp.StageName ='Closed Won';
        objopp.CloseDate = system.today();
        objopp.Mode_of_Contact__c = 'Telephone';
        objopp.AccountId = objacc.Id;
        insert objopp;
        
        Booking__c objbook = New Booking__c();
        objbook.Name = 'Test';
        objbook.Opportunity__c = objopp.Id;
        lstbooking.add(objbook);
        insert lstbooking;
        
      		list<ContentVersion> conVers = new list<ContentVersion>();
       		ContentVersion ContVers = new ContentVersion();
            ContVers.Title = 'Testing multiple att ';
            ContVers.PathOnClient = 'Carrots.jpg';
            ContVers.VersionData = Blob.valueOf('Carrots');
            ContVers.IsMajorVersion = true;
            conVers.add(ContVers);
            insert conVers;
            map<Id, ContentVersion> contMap = new map<Id, ContentVersion>(conVers);
        
         List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument WHERE LatestPublishedVersionId IN :contMap.KeySet()];
            ContentDocumentLink ConLink = new ContentDocumentLink();
            ConLink.LinkedEntityId = objopp.Id; 
            ConLink.ContentDocumentId = documents[0].Id;
            ConLink.shareType = 'V';
            lstcon.add(ConLink);
        insert lstcon;

    }
}

Hii ricky I modified my test class but not cover
Ricky Lowe 19Ricky Lowe 19
Hi Bablu,

Your trigger relies on the Content Link being associated with the Opportunity before you insert the Booking__c. Try changing the order of the test code so that the ContentVersion and ConentDocumentLink are before the insert of the Booking.

Cheers,

Ricky
David Zhu 🔥David Zhu 🔥
My understanding of your code is you are trying to attach content note from parent Opportunity to Booking record.
then you need to create contont note before insert booking record.
You may use the following code snipet.
@isTest
public class BookingTriggerHandlerTest{
    static testMethod void Testdata(){
        List<ContentDocumentLink> lstcon = New List<ContentDocumentLink>();
        List<Booking__c> lstbooking = New List<Booking__c>();
        Account objacc = New Account();
        objacc.name = 'Test';
        objacc.Type = 'Press';
        insert objacc;

        Opportunity objopp = new Opportunity();
        objopp.name ='Test';
        objopp.StageName ='Closed Won';
        objopp.CloseDate = system.today();
        //objopp.Mode_of_Contact__c = 'Telephone';
        objopp.AccountId = objacc.Id;
        insert objopp;
        
        List<ContentVersion> lstnote = New List<ContentVersion>();
        ContentVersion objnote = New ContentVersion();
        objnote.title = 'Test';
        objnote.PathOnClient = objnote.Title + '.snote';
        objnote.VersionData = Blob.valueof('this is a test');
        objnote.FirstPublishLocationId = objopp.Id;
        
        lstnote.add(objnote);
        insert lstnote;

        Booking__c objbook = New Booking__c();
        objbook.Name = 'Test';
        objbook.Opportunity__c = objopp.Id;
        lstbooking.add(objbook);
        insert lstbooking;
        

        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument where Id =:objopp.Id];
        if(!documents.isEmpty()){
           ContentDocumentLink contentlink=new ContentDocumentLink();
		   contentlink.ShareType= 'C';
		   contentlink.LinkedEntityId = objopp.Id;
		   contentlink.ContentDocumentId=documents[0].Id;
		   contentlink.Visibility = 'AllUsers';
           lstcon.add(contentlink);
           System.assertEquals(documents.size(), 1);
        }
		insert lstcon;
    }
}


 
This was selected as the best answer
Bablu Kumar PanditBablu Kumar Pandit
Thanku so much David,it Worked