1

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);
    }

}

Business Hours ScreenShot:Business Hours

1

You would want to use the BusinessHours.isWithin method, which requires a Datetime. You could do something as simple as:

public with sharing class LeadSevice
{
    static BusinessHours defaultHours
    {
        get
        {
            if (defaultHours == null)
                defaultHours = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
            return defaultHours;
        }
        private set;
    }
    public static Boolean isWithinBusinessHours(Lead record)
    {
        Datetime toVerify = (record.CreatedDate == null) ?
            Datetime.now() : record.CreatedDate;
        // above would be necessary if you want this functionality
        // to work in an insert trigger
        return BusinessHours.isWithin(defaultHours.Id, toVerify);
    }
}

Once that's set up, your original statement (which looks like a check that you are outside hours) would be replaced with:

if (!LeadService.isWithinBusinessHours(leadObj))
    // do stuff based on being outside business hours
else
    // do stuff based on being inside business hours
  • so there needs to be a business hours record already created? what is record.CreatedDate? – Bryan Cerrati Oct 19 '16 at 17:55
  • then i would just ask if(BusinessHours.isWithin(defaultHours.Id, toVerify); – Bryan Cerrati Oct 19 '16 at 17:57
  • The joys of descriptive naming. It tells you if the specified Datetime is within business hours. And record.CreatedDate tells you when the record was created. Yes, you would have to set up a BusinessHours record, but there should be one already set up, I think. – Adrian Larson Oct 19 '16 at 18:42
  • so where Lead is i should put my custom object? – Bryan Cerrati Oct 19 '16 at 19:02
  • @Bryan Right...forgot you are using a custom object. Yes, you would change that to reflect the actual sObjectType you are working with. – Adrian Larson Oct 19 '16 at 19:53

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.