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
Paul 32Paul 32 

Struggling with test class for insert trigger

Hi I have the following trigger that sits on leads, what it does is clones a files and attaches it to the lead. I need to write a test class, but I can only cover 45%, the part that isnt covered is the ContentVersion insert. Any help would be appreciated. 

trigger copyFiles on Lead (after insert) {       
    
    Lead lead = [Select AttachmentId__c From Lead where Id IN : Trigger.new];
    
    if(lead.AttachmentId__c == Null){ 
        //Do Nothing         
    } else { 
        
        Lead l = [Select Id, Patient_Referral_Id__c, AttachmentId__c From Lead where Id IN : Trigger.new];          
        
        ContentVersion  cont = [SELECT Checksum,ContentDocumentId,ContentLocation,ContentSize,ContentUrl,
                                Description,FileExtension,FileType,FirstPublishLocationId,Id,IsAssetEnabled,
                                IsDeleted,Origin,OwnerId,PathOnClient,PublishStatus,RatingCount,ReasonForChange,
                                SharingOption,Title,VersionData,VersionNumber 
                                FROM ContentVersion Where ContentDocumentId =: l.AttachmentId__c];          
        
            ContentVersion newcont = new ContentVersion();
            newcont.Title  = cont.Title;
            newcont.PathOnClient  = cont.PathOnClient;
            newcont.VersionData = cont.VersionData;
            newcont.FirstPublishLocationId  = l.Id;            
            insert newcont;      
    } 
}
Raj VakatiRaj Vakati
@isTest
private class aaaaaaaaaaaaaa_Test {
    private static testMethod void testMethod() 
    { 
        Attachment att = new Attachment();
        
        
        
        Attachment attach=new Attachment();   	
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        //attach.parentId=l.id;
        insert attach;
        //i.AttachmentId__c =attach.Id ;
        //update l;
        
        
        Lead l=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Inquiry' ,AttachmentId__c =attach.Id);
        insert l;
        
        
        ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        //content.LinkedEntityId=sub.id;
        content.origin = 'H';
        insert content;
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=l.id;
        contentlink.contentdocumentid=content.contentdocumentid;
        insert contentlink;
        
        List<ContentDocumentLink> files=[SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, LinkedEntityId, ContentDocument.Title 
                                         from ContentDocumentLink where ContentDocumentId= :content.ContentDocumentId]; 
      //  System.assertEquals(1, files.size()); 
    }
}

 
Paul 32Paul 32
Hi Raj V, thanks for your reply, unfortunatley this gets 0% coverage of the trigger. I also tried to insert the attachment before the lead, but this always results in 0 coverage.