1

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 = 'testemail@yahoo.com',
            username = 'testemail@yahoo.com', 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'); 

    }
}
1

The root cause of this is that both of your queries are returning 0 rows. This means that the code within the for loops and the if statements are never being run.

There could be two reasons for this:

  1. There really are no records to pull.

  2. You are using API version 24 or greater which does not allow you (by default) to query live data.

I think that you are most likely running into reason #2. To fix this, you can either change your code to create the records before you query for them, or you can simply grant permission to your test class to query live data. You can do this by adding (SeeAllData=true) after your @isTest tag, like so:

@isTest(SeeAllData=true)
  • No problem at all, this definitely snagged me a couple of weeks ago.. – VictorKilo Jul 18 '13 at 16:02
1

Looks like your SOQL queries at lines 11 & 16 aren't returning any records, thus the for loops are never entered, and the lists you check size of at lines 22 & 26 are empty.

  • Can this be covered? – McKev Jul 18 '13 at 15:41
  • 1
    You just need to update your test class to make the test records meet the criteria for the two SOQL queries. I would suggest putting in some debug statements to check for all the relevant values, to make sure there are no oversights there. – Davin Casey Jul 18 '13 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.