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?
null
records...for
loops you are checkingl != null
andl2 != null
. Those are alwaystrue
. Records returned from a query can never benull
.