You need to sign in to do that
Don't have an account?
Bablu 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; } }
then you need to create contont note before insert booking record.
You may use the following code snipet.
All Answers
To test the insert of a ContentDocumentLink you need to first insert a ContentVersion. See below for test class method to create ContentDocumentLink:
Cheers,
Ricky
Hii ricky I modified my test class but not cover
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
then you need to create contont note before insert booking record.
You may use the following code snipet.