+ Start a Discussion
Swetha B 7Swetha B 7 

Code Coverage Helper

Trigger
 
trigger Attchementsharing on ContentDocumentLink (before insert) 
{
    Set<Id> Ids = new Set<Id>();
    for (ContentDocumentLink cdl : trigger.new) {
        String id = cdl.LinkedEntityId;
        if (id.substring(0,3) == '02s') {
            Ids.add(Id);   
        }
        else if (id.substring(0,3) == '500'&& cdl.visibility !='AllUsers') {
            cdl.visibility ='AllUsers';
        }     
    } 
    Map<ID, EmailMessage> m = new Map<ID, EmailMessage>([SELECT Id, RelatedToId FROM EmailMessage where Id IN:Ids]);
    List<ContentDocumentLink> docs = new  List<ContentDocumentLink>();
    for (ContentDocumentLink cdl : trigger.new) {
        String id = cdl.LinkedEntityId;
        if (id.substring(0,3) == '02s') {
            ContentDocumentLink sr = new ContentDocumentLink();
            sr.sharetype = 'I';
            sr.visibility = 'AllUsers';
            sr.ContentDocumentId = cdl.ContentDocumentId;
            sr.LinkedEntityId = m.get(Id).RelatedToId ;
            docs.add(sr);
        }
    }
    If (docs.size()>0){
        
        Insert docs;
    }  
}
Test Class: Current Code Coverage is 55%, Please help me to get 100%
 
@IsTest
public class Attachementsharing_Test {
    
    static testmethod void testmethod1(){
        Test.startTest();
        Case cs = new Case(SuppliedName='test Case',SuppliedEmail='Test@test.com', Category__c='Client');
        insert cs;
        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 sr=new ContentDocumentLink();
        sr.LinkedEntityId=cs.id;
        sr.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        sr.ShareType = 'I';
        sr.Visibility = 'AllUsers'; 
        insert sr;
        Test.stopTest();
        
    }
}

 
Best Answer chosen by Swetha B 7
David Zhu 🔥David Zhu 🔥
You may try this:
@IsTest
public class Attachementsharing_Test {
    
    static testmethod void testmethod1(){
        Test.startTest();
        Case cs = new Case(SuppliedName='test Case',SuppliedEmail='Test@test.com', Category__c='Client');
        insert cs;
        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;
        
        
        EmailMessage emailMessage = new EmailMessage();
        emailMessage.status = '3'; 
        emailMessage.relatedToId = cs.Id;
        emailMessage.fromAddress = 'sender@example.com'; 
        emailMessage.fromName = 'Example Sender'; 
        emailMessage.subject = 'This is the Subject!'; 
        emailMessage.htmlBody = '<html><body><b>Hello</b></body></html>'; 
        emailMessage.toAddress = 'emailnotinsalesforce@toexample.com, anotherone@toexample.com';
        insert emailMessage; 
        
        ContentDocumentLink sr=new ContentDocumentLink();
        sr.LinkedEntityId=emailMessage.id;
        sr.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        sr.ShareType = 'I';
        sr.Visibility = 'AllUsers'; 
        insert sr;

        Test.stopTest();
        
    }
}

 

All Answers

David Zhu 🔥David Zhu 🔥
how do you share the attachment  in salesforce ui to object with id starting with 02s?
Swetha B 7Swetha B 7
Hi  David,

Appreciate your response!

Yes, 02S id for an Email Message object. Please let me know how to pass the substring in the test class.

Kindly help with my trigger code coverage.

Thank you,
Swetha
David Zhu 🔥David Zhu 🔥
You may try this:
@IsTest
public class Attachementsharing_Test {
    
    static testmethod void testmethod1(){
        Test.startTest();
        Case cs = new Case(SuppliedName='test Case',SuppliedEmail='Test@test.com', Category__c='Client');
        insert cs;
        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;
        
        
        EmailMessage emailMessage = new EmailMessage();
        emailMessage.status = '3'; 
        emailMessage.relatedToId = cs.Id;
        emailMessage.fromAddress = 'sender@example.com'; 
        emailMessage.fromName = 'Example Sender'; 
        emailMessage.subject = 'This is the Subject!'; 
        emailMessage.htmlBody = '<html><body><b>Hello</b></body></html>'; 
        emailMessage.toAddress = 'emailnotinsalesforce@toexample.com, anotherone@toexample.com';
        insert emailMessage; 
        
        ContentDocumentLink sr=new ContentDocumentLink();
        sr.LinkedEntityId=emailMessage.id;
        sr.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        sr.ShareType = 'I';
        sr.Visibility = 'AllUsers'; 
        insert sr;

        Test.stopTest();
        
    }
}

 
This was selected as the best answer
Swetha B 7Swetha B 7
Thank you very much David!