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
TinkerMeTinkerMe 

Help with Test Coverage Updating checkboxes from Attachment to Parent Record

Hi Experts,

Salesforce NewB here. I just need some help reaching 100% code coverage. I am only getting 34% Code Coverage.

My goal of my trigger is to update the checkbox on the Lead record based on the Attachment Name.

Trigger:
trigger OnboardingDocumentCheckboxes on Attachment (after insert, before delete, after delete) 
{  
    if(trigger.isinsert){        
        List<Lead> lead = [select id, 
                           Box1_Uploaded__c, 
                           Box2_Uploaded__c,
                           Box3_Uploaded__c
                           from Lead where id =: Trigger.New[0].ParentId];          
        
        if(lead.size()>0)          
        {
             for(Attachment att : trigger.New){
                    if(att.Name.startsWith('TEST1')){
                    lead[0].Box1_Uploaded__c = True;
                } else if (att.Name.startsWith('TEST2')){
                    lead[0].Box2_Uploaded__c = True;
                } else if (att.Name.startsWith('TEST3')){
                    lead[0].Box3_Uploaded__c = True;
                }
            update lead;          
        } 
      }
    }
    if(trigger.isdelete){        
        List<Lead> lead = [select id,
                           Box1_Uploaded__c, 
                           Box2_Uploaded__c,
                           Box3_Uploaded__c                    
                           from Lead where id =: Trigger.old[0].ParentId]; 
        
        if(lead.size()>0)          
        {  
            List<Attachment> attach = [Select id from Attachment where parentid = :lead[0].id]; 
            for(Attachment att : trigger.Old){          
                    if(att.Name.startsWith('TEST1')){
                    lead[0].Box1_Uploaded__c = True;
                } else if (att.Name.startsWith('TEST2')){
                    lead[0].Box2_Uploaded__c = True;
                } else if (att.Name.startsWith('TEST3')){
                    lead[0].Box3_Uploaded__c = True;
                }            
            update lead;          
        } 
    }
}
}
TEST CLASS:
// All test methods in this class can access all data.

@isTest(SeeAllData=true)
public class TestOnboardingDocumentCheckboxes {

    static testMethod void testAttachments() {
        Lead testLead = new Lead();
        testLead.Company = 'Test Company';
        testLead.LastName= 'TestLastName';
        testLead.Lead_Status_NEW__c = 'New';
        testLead.Box1_Uploaded__c=true;
        testLead.Box2_Uploaded__c=true;
        testLead.Box3_Uploaded__c=true;
        insert testLead;
    
    Attachment attach=new Attachment();
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('UnitTest Attachment Body');
        attach.body=bodyBlob;
        attach.ParentId=testLead.id;
        insert attach;  
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:testLead.id]; 
        System.assertEquals(1, attachments.size()); 
    }
}
I would appreciate any help. Thank you.
 
Sitarama MurthySitarama Murthy
hi TinkerMe,

@isTest(SeeAllData=true)
public class TestOnboardingDocumentCheckboxes {
    
    // All test methods in this class can access all data.



    static testMethod void testAttachments() {
        Lead testLead = new Lead();
        testLead.Company = 'Test Company';
        testLead.LastName= 'TestLastName';
      //  testLead.Lead_Status_NEW__c = 'New';
        testLead.Box1_Uploaded__c=true;
        testLead.Box2_Uploaded__c=true;
        testLead.Box3_Uploaded__c=true;
        insert testLead;
        
        
    List<Attachment> atts = new List<Attachment>();
        for(Integer i=1;i<=3;i++){
            Attachment attach=new Attachment();
            attach.Name='TEST'+i;
            Blob bodyBlob=Blob.valueOf('UnitTest Attachment Body');
            attach.body=bodyBlob;
            attach.ParentId=testLead.id;
            atts.add(attach);
        }
        insert atts;  
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:testLead.id]; 
        //System.assertEquals(1, attachments.size()); 
        
        delete attachments;
    }


}

use this test class..
TinkerMeTinkerMe
Hi Ram,

Thanks for your help. It improves the code coverage but it still only up by 65%.
TinkerMeTinkerMe
can we still bump it to 100%? thank you.
Sitarama MurthySitarama Murthy
hi,
if u have used same code which i given , then u should get 100% coverage.
when i tried i got 100% coverage
TinkerMeTinkerMe
Hi Ram,

I've tried to to use the exact same code above with same APIs on another developer org and it seems that its on 100% code coverage. But when I apply same logic on my real code, I am only getting 65%. Can you help me with this?

TRIGGER:
trigger OnboardingDocumentCheckboxes on Attachment (after insert, before delete, after delete) 
{  
    if(trigger.isinsert){        
        List<Lead> lead = [select id, 
                           CIN_Uploaded__c, POA_Uploaded__c,
                           SLA_Uploaded__c,
                           TPA_Uploaded__c, NRI_Uploaded__c,
                           Additional_Info_Sheet_Uploaded__c, Letter_of_Instruction_Uploaded__c
                           from Lead where id =: Trigger.New[0].ParentId];          
        
        if(lead.size()>0)          
        {
             for(Attachment att : trigger.New){
                    if(att.Name.startsWith('CIN')){
                    lead[0].CIN_Uploaded__c = True;
                } else if (att.Name.startsWith('POA')){
                    lead[0].POA_Uploaded__c = True;
                } else if (att.Name.startsWith('SLA')){
                    lead[0].SLA_Uploaded__c = True;
                } else if (att.Name.startsWith('TPA')){
                    lead[0].TPA_Uploaded__c = True;
                } else if (att.Name.startsWith('NRI')){
                    lead[0].NRI_Uploaded__c = True;
                } else if (att.Name.startsWith('Additional Info')){
                    lead[0].Additional_Info_Sheet_Uploaded__c = True;
                } else if (att.Name.startsWith('Letter of Instruction')){
                    lead[0].Letter_of_Instruction_Uploaded__c = True;
                }
            update lead;          
        } 
      }
    }
    if(trigger.isdelete){        
        List<Lead> lead = [select id,
                           CIN_Uploaded__c, POA_Uploaded__c,
                           SLA_Uploaded__c,
                           TPA_Uploaded__c, NRI_Uploaded__c,
                           Additional_Info_Sheet_Uploaded__c, Letter_of_Instruction_Uploaded__c                           
                           from Lead where id =: Trigger.old[0].ParentId]; 
        
        if(lead.size()>0)          
        {  
            List<Attachment> attach = [Select id from Attachment where parentid = :lead[0].id]; 
            for(Attachment att : trigger.Old){          
                        if(att.Name.startsWith('CIN')){
                    lead[0].CIN_Uploaded__c = False;
                } else if (att.Name.startsWith('POA')){
                    lead[0].POA_Uploaded__c = False;
                } else if (att.Name.startsWith('SLA')){
                    lead[0].SLA_Uploaded__c = False;
                } else if (att.Name.startsWith('TPA')){
                    lead[0].TPA_Uploaded__c = False;
                } else if (att.Name.startsWith('NRI')){
                    lead[0].NRI_Uploaded__c = False;
                } else if (att.Name.startsWith('Additional Info')){
                    lead[0].Additional_Info_Sheet_Uploaded__c = False;
                } else if (att.Name.startsWith('Letter of Instruction')){
                    lead[0].Letter_of_Instruction_Uploaded__c = False;
                }            
            update lead;          
        } 
    }
}
}

TEST CLASS:
 
@isTest(SeeAllData=true)
public class TestOnboardingDocumentCheckboxes {

// All test methods in this class can access all data.
    static testMethod void testAttachments() {
        Lead testLead = new Lead();
        testLead.Company = 'Test Company';
        testLead.LastName= 'TestLastName';
        // testLead.Lead_Status_NEW__c = 'New';
        testLead.CIN_Uploaded__c=true;
        testLead.POA_Uploaded__c=true;
        testLead.SLA_Uploaded__c=true;
        testLead.TPA_Uploaded__c=true;
        testLead.NRI_Uploaded__c=true;
        testLead.Additional_Info_Sheet_Uploaded__c=true;
        testLead.Letter_of_Instruction_Uploaded__c=true;        
        insert testLead;
    
   List<Attachment> att = new List<Attachment>();
    for(Integer i=1;i<=7;i++){
        Attachment attach=new Attachment();
            attach.Name='Unit Test Attachment'+i;
            Blob bodyBlob=Blob.valueOf('UnitTest Attachment Body');
            attach.body=bodyBlob;
            attach.ParentId=testLead.id;
            att.add(attach);  
                }
        insert att;  
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:testLead.id]; 
        // System.assertEquals(1, attachments.size()); 
        
          delete attachments;
    }
}

 
Sitarama MurthySitarama Murthy
hi,

In test class try inserting attachments having name same as you given in trigger for condition check.
you will get 100%coverage 
TinkerMeTinkerMe
Hi Ram,

Sorry. I am bit lost. would you be able to guide me? Should it be,  attach.Name='CIN'+i; ?

Thank you.