You need to sign in to do that
Don't have an account?
TinkerMe
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:
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.
@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..
Thanks for your help. It improves the code coverage but it still only up by 65%.
if u have used same code which i given , then u should get 100% coverage.
when i tried i got 100% coverage
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:
TEST CLASS:
In test class try inserting attachments having name same as you given in trigger for condition check.
you will get 100%coverage
Sorry. I am bit lost. would you be able to guide me? Should it be, attach.Name='CIN'+i; ?
Thank you.