Need assistance to identify what's needed to obtain better coverage, which is currently 62%. Lines 12, 13, 17, 18, 23, 27 are all not covered. To note: I have not included any references to Contact__c in my test just yet.
Trigger:
line source
1 trigger WAPrefCenterRecent on WebActivity__c (before insert) {
2
3 List<WebActivity__c> contactrecords = new List<WebActivity__c>();
4 List<WebActivity__c> leadrecords = new List<WebActivity__c>();
5
6 for(WebActivity__c r: trigger.new){
7 String c = r.Contact__c;
8 String l = r.Lead__c;
9 String i = r.Id;
10
11 for (WebActivity__c contactlist : [select Contact__c from WebActivity__c where Contact__c = :c AND Id != :i AND IsMostRecent__c = 'Yes' AND RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Preference Center')]){
12 contactlist.IsMostRecent__c = 'No';
13 contactrecords.add(contactlist);
14 }
15
16 for (WebActivity__c leadlist : [select Lead__c from WebActivity__c where Lead__c = :l AND Id != :i AND IsMostRecent__c = 'Yes' AND RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Preference Center')]){
17 leadlist.IsMostRecent__c = 'No';
18 leadrecords.add(leadlist);
19 }
20 }
21
22 if(contactrecords.size()>0) {
23 update contactrecords;
24 }
25
26 if(leadrecords.size()>0) {
27 update leadrecords;
28 }
29
30 }
Test:
@isTest
public class WAPrefCenterRecent_Trigger_Test {
static testMethod void runCase() {
//get standard profile
Profile p = [SELECT id from profile WHERE name = 'Standard User'];
//insert a new user
User u = new User (lastname = 'testlast', alias = 'tstalias', email = '[email protected]',
username = '[email protected]', profileid = p.Id, timezonesidkey = 'America/Chicago',
localesidkey = 'en_US', emailencodingkey = 'UTF-8', languagelocalekey = 'en_US');
insert u;
//insert a new lead
Lead l = new Lead (lastname = 'testlast', company = 'test company', ownerid = u.id);
insert l;
List<WebActivity__c> leadrecords = new List<WebActivity__c>();
//insert web activities related to previously inserted lead
WebActivity__c wal1 = new WebActivity__c(lead__c = l.Id, IsMostRecent__c = 'Yes');
insert wal1;
for (WebActivity__c leadlist : [select Lead__c from WebActivity__c where Id = :wal1.id ]){
leadlist.IsMostRecent__c = 'No';
leadrecords.add(leadlist);
}
update leadrecords;
WebActivity__c wal1q = [ SELECT Id, IsMostRecent__c FROM WebActivity__c
WHERE Id = :wal1.Id ];
System.assertEquals(wal1q.IsMostRecent__c, 'No');
WebActivity__c wal2 = new WebActivity__c(lead__c = l.Id, IsMostRecent__c = 'Yes', WebSource__c = 'test-two');
insert wal2;
WebActivity__c wal2q = [ SELECT Id, IsMostRecent__c FROM WebActivity__c
WHERE Id = :wal2.Id ];
System.assertEquals(wal2q.IsMostRecent__c, 'Yes');
}
}