i had a post but accidentally deleted my account -hits Head-
the original post contained my question of how do i test createdDate.hour() if i cannot modify the createddate even to test an account creation.
someone led me down a different path, to test using the Test.isRunningTest() method in the trigger to allow the test to enter the main if statement without challenging the created date. but we were unfinished in resolving the problem.
private void sendAlertsPrivate(List<JungoLeadsFrc__JungoLeads__c> lstNewLead, Boolean passTestDate)
{
List<JungoLeadsFrc__JungoLeads__c> lstLeadToUpdate = new List<JungoLeadsFrc__JungoLeads__c>();
List<Contact> lstTempContact = new List<Contact>();
List<Task> lstTaskToInsert = new List<Task>();
List<Messaging.SingleEmailMessage> lstMail = new List<Messaging.SingleEmailMessage>();
User addUser = [SELECT Alias,Email,Id FROM User WHERE Name = 'Jason Frangoulis' LIMIT 1];
for(JungoLeadsFrc__JungoLeads__c objLead: lstNewLead)
{
Integer objCreatedDate = objLead.CreatedDate.hour();
if(passTestDate)
{
if(objLead.JungoLeadsFrc__Zillow_Recipient_Email__c != NULL)
{
objCreatedDate = 10;
}
else
{
objCreatedDate = 20;
}
}
if(objLead.JungoLeadsFrc__Zillow_Recipient_Email__c != null)
{
lstLeadToUpdate.add(new JungoLeadsFrc__JungoLeads__c(
Id = objLead.Id,
OwnerId = addUser.Id,
JungoLeadsFrc__LeadSource__c = 'Zillow',
JungoLeadsFrc__Stage__c = 'New'));
}
else
{
lstLeadToUpdate.add(new JungoLeadsFrc__JungoLeads__c(
Id = objLead.Id,
OwnerId = addUser.Id,
JungoLeadsFrc__LeadSource__c = 'Personal',
JungoLeadsFrc__Stage__c = 'New'));
}
if(objCreatedDate >= 18 || objCreatedDate <= 8 || passTestDate)
{
//Task: After Hours ALERT
//Email
if(objLead.JungoLeadsFrc__EmailAddress__c != null)
{
//email html
}
else
{
//Email if No Email Address to SF ADMINS
}
}
else
{
//Task "Call New Lead"
// Task "NEW LEAD RECIEVED"
if(objLead.JungoLeadsFrc__EmailAddress__c != null)
{
//Email if recieved 9-5pm
}
else
{
//Email if NO Email Address
}
}
//send email
if(!lstMail.isEmpty())
{
Messaging.sendEmail(lstMail);
}
//Insert Tasks
if(!lstTaskToInsert.isEmpty())
{
insert lstTaskToInsert;
}
//update Record
if(!lstLeadToUpdate.isEmpty())
{
update lstLeadToUpdate;
}
}
and the Trigger:
trigger JungoLeadTrigger on JungoLeadsFrc__JungoLeads__c(after insert){
JungoLeadFollowup objHandler = new JungoLeadFollowup();
if(Trigger.isAfter && Trigger.isInsert){
if(Test.isRunningTest()) {
objHandler.leadAlerts(Trigger.New, true);
}
else {
objHandler.leadAlerts(Trigger.New, false);
}
}
i get about 48% code coverage now. creating records with all 4 combinations of having and not null emailAddress and zillow_recipient_email__c being null and not null. so im in need of help.
Test.setCreatedDate:
@isTest static void sixToNine()
{
JungoLeadsFrc__JungoLeads__c sixToNine = new JungoLeadsFrc__JungoLeads__c(
JungoLeadsFrc__EmailAddress__c = '[email protected]');
insert sixToNine;
Test.setCreatedDate(sixToNine.Id, DateTime.newInstance(2016, 8, 21, 19, 0, 0));
Test.startTest();
JungoLeadsFrc__JungoLeads__c jungo = [SELECT Id, CreatedDate FROM JungoLeadsFrc__JungoLeads__c WHERE JungoLeadsFrc__EmailAddress__c = '[email protected]'];
System.assertEquals(sixToNine.CreatedDate, DateTime.newInstance(2016, 8, 21, 19, 0, 0));
Test.stopTest();
}
Test.setCreatedDate does not execute anything in the primary if statement. this is the original 6pm to 8.59am code block.
if(objLead.CreatedDate.hour() >= 18 || objLead.CreatedDate.hour() <= 8)
a.Id
, say, afterTest.startTest()
what time does it show?BusinessHours
once you get a handle on what you have so far.before
event, notafter
.