I have a main if
statement in my class that utilizes CreatedDate.hour()
to execute certain code between 6pm and 8.59am and the ELSE is 9am to 5.59pm which would be within business hours if I could get this working.
The if statement goes like this:
if(objLead.CreatedDate.hour() >= 18 || objLead.CreatedDate.hour() <= 8)
// outside hours logic
else
// inside hours logic
I would like to change this to use BuisnessHours
. How would that class be used here?
updated code: Class:
public with sharing class jungoLeadFollowup{
public void leadAlerts(List<JungoLeadsFrc__JungoLeads__c> lstNewLead){
sendAlertsPrivate(lstNewLead);
}
private void sendAlertsPrivate(List<JungoLeadsFrc__JungoLeads__c> lstNewLead)
{
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)
{
if(!jungoLeadHours.isWithinBusinessHours(objLead))
{
//Task: After Hours ALERT
//Email
if(objLead.JungoLeadsFrc__EmailAddress__c != null)
{
//Email: Off Hours
}
else
{
//Email: off Hours: to admin
}
}
else
{
//Task "Call New Lead"
// Task "NEW LEAD RECIEVED"
if(objLead.JungoLeadsFrc__EmailAddress__c != null)
{
//Email: on hours
}
else
{
//Email: on hours: to admin
}
}
}
//send email
if(!lstMail.isEmpty())
{
Messaging.sendEmail(lstMail);
}
//Insert Tasks
if(!lstTaskToInsert.isEmpty())
{
insert lstTaskToInsert;
}
//update Record
if(!lstLeadToUpdate.isEmpty())
{
update lstLeadToUpdate;
}
}
Trigger:
trigger JungoLeadTrigger on JungoLeadsFrc__JungoLeads__c(after insert){
JungoLeadFollowup objHandler = new JungoLeadFollowup();
if(Trigger.isAfter && Trigger.isInsert)
{
objHandler.leadAlerts(Trigger.New);
}
}
jungoLeadHours class: (renamed it):
public with sharing class jungoLeadHours{
public static BusinessHours defaultHours{
get
{
if(defaultHours == null)
{
defaultHours = [SELECT Id from BusinessHours WHERE isDefault = true];
return defaultHours;
}
else
{
return null;
}
}
private set;
}
public static Boolean isWithinBusinessHours(JungoLeadsFrc__JungoLeads__c record)
{
DateTime toVerify = (record.CreatedDate == null) ? DateTime.now() : record.CreatedDate;
return BusinessHours.isWithin(defaultHours.Id, toVerify);
}
}