1

I have a class that updates leads and sends emails to the Lead owners, this part works just fine. Now that I'm writing the Test class, in my test the lead I create doesn't get updated. Why could that be? Also, I'm not sure how to test the message sent in my test. I'm doing a System Debug to show the values are correct and that the record meets the criteria to get updated, it all looks good until I do the System.AssertEquals and the value I expect is wrong. Any help would be amazing, this is the code:

My class:

public static void execute(){

    List<Lead> leadlist=new List<Lead>([SELECT Id,OwnerId,Owner.Name, Owner.Email From Lead WHERE (Status='Active' OR Status='Sales Qualified') AND Is_in_queue__c = False AND
                                     IsConverted = False]);
    List<Lead> leadlist2=new List<Lead>([SELECT Id,OwnerId,Owner.Name, Owner.Email From Lead WHERE (Status='Active' OR Status='Sales Qualified') AND Is_in_queue__c = False AND IsConverted = False]);
    System.debug('Size of the list:'+leadlist.size());


    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();


     for (Lead l:leadlist){
        if (l != null) {
          Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();

          List<String> sendTo = new List<String>();
          sendTo.add(l.Owner.Email);
          mail.setToAddresses(sendTo);

          mail.setReplyTo('[email protected]');
          mail.setSenderDisplayName('Salesforce Support');


          mail.setSubject('Your Lead will become inactive in 7 days');
          String body = '' + l.Owner.Name + ', <br/>';

           mail.setHtmlBody(body);
           mails.add(mail); 
        }
     }

    for (Lead l2:leadlist){
        if (l2 != null) {
          Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
          l2.Status='Inactive';
          List<String> sendTo = new List<String>();
          sendTo.add(l2.Owner.Email);
          mail.setToAddresses(sendTo);

          mail.setReplyTo('[email protected]');
          mail.setSenderDisplayName('Salesforce Support');


          mail.setSubject('Your Lead is now inactive');
          String body = '' + l2.Owner.Name + ', <br/>';
           body+='Here is a link for you to see the lead that has changed to an Inactive Status:  blah blah'<br/>';

           mail.setHtmlBody(body);
           mails.add(mail); 
        }
      }
    System.debug('Size of the list of Leads: '+mails.size());
    Messaging.sendEmail(mails);
    sizeNumber = mails.size();
}
}

My test class:

@isTest
public class InactiveLeadEmailTest {

public testmethod static void updateLeadtoInactive(){
        User adminUser = sObjectCreator.createUser();
        adminUser.ProfileId = sObjectCreator.systemAdminUserProfileId;
        insert adminUser;

    lead l = sObjectCreator.createLead();
    l.User_Last_Modified_Date__c = System.today().addDays(-89);
    l.Status='Active';
    l.OwnerId = adminUser.id;
    l.IsConverted=False;
    insert l;
    System.debug('Lead Id: '+ l.Id);

    Test.startTest();
    InactiveLeadEmail.execute();
    Test.stopTest();

    //Integer i = InactiveLeadEmail.sizeNumber();        

    List<Lead> leadlist = new List<Lead>();
    leadlist= [SELECT Id, Status,User_Last_Modified_Date__c, DB_Lead_Age__c FROM Lead WHERE Id=: l.Id LIMIT 1];
    update leadlist;
    System.debug('User LastMod: '+leadlist[0].User_Last_Modified_Date__c);
    System.debug('Lead Status: '+leadlist[0].Status);
    System.debug('DB Lead Age: '+leadlist[0].DB_Lead_Age__c);

    System.assertEquals('Inactive', leadlist[0].Status);

}
}

What am I doing wrong?

4
  • 2
    Queries cannot return null records...
    – Adrian Larson
    Commented Dec 22, 2015 at 22:32
  • What do you mean?
    – Leo
    Commented Dec 22, 2015 at 22:35
  • 1
    In your for loops you are checking l != null and l2 != null. Those are always true. Records returned from a query can never be null.
    – Adrian Larson
    Commented Dec 22, 2015 at 23:16
  • 1
    @Leo as suggested from sfdcFox below, when you submit code for a question try and clean it up(refactor) before submitting. 95% of the time people who know the answer won't even bother if you have more than the bare bones to what you're trying to accomplish.
    – kwiknick
    Commented Dec 23, 2015 at 3:45

1 Answer 1

6

I believe the answer is simple (without doing a deep look):

In your execute method you never update leadList2 after you set the value to inactive...

Add

update leadList2;

to the end of your execute method

Also: Why do are you using two queries / lists that are exactly the same?

4
  • 6
    ... And why are they checking if the lead is null from a query? And why aren't they emailing a user by setTargetObjectId so they don't waste mass mail limits? And why are they constructing a list from an inline query, which already returns a list? And why are there so many debug statements? And what's with wasting all those extra variables? ... my head hurts.
    – sfdcfox
    Commented Dec 22, 2015 at 22:42
  • 4
    @sfdcfox - lol. thats why I did not address them. Stuck to the immediate question, all those issues are "Too Broad" lol
    – Eric
    Commented Dec 22, 2015 at 22:43
  • Originally there were 2 different queries and 2 different lists: List<Lead> leadlist=new List<Lead>([SELECT Id,OwnerId,Owner.Name, Owner.Email From Lead WHERE (Status='Active' OR Status='Sales Qualified') AND Is_in_queue__c = False AND DB_Lead_Age__c = 83 AND IsConverted = False]); List<Lead> leadlist2=new List<Lead>([SELECT Id,OwnerId,Owner.Name, Owner.Email From Lead WHERE (Status='Active' OR Status='Sales Qualified') AND Is_in_queue__c = False AND DB_Lead_Age__c = 90 AND IsConverted = False]); Eric's solution fixed the issue. Now I need to clean the code as it was. Thanks Eric!
    – Leo
    Commented Dec 22, 2015 at 22:54
  • 3
    @Leo - If this worked for you please mark the answer as the correct one so others with a similar can find answered questions and so it does not keep surfacing as unanswered
    – Eric
    Commented Dec 22, 2015 at 23:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .