function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aaron Olson 26Aaron Olson 26 

Business Hour trigger on lead

I am looking for some help with a test class. My code looks at when a lead comes in and creates a taks with an after business hours flag. I am trying creat a test class that uses business hours to test the following code. I havnt figured out a way to simulate after hours. 
 
trigger Working_Lead_business on Lead (after insert) {
    public datetime mydate = system.now();
    List<Task> tsk_lst = new list<task>{};
    BusinessHours bh = [select id from businesshours where id = '01m50000000LjJ5'];
    Boolean isWithin = BusinessHours.isWithin(bh.id, mydate);
    system.debug('BusinessHours = ' + isWithin);
    
    For(Lead l: Trigger.new) {
        if(l.Status == 'New' && isWithin == false && (l.Country != 'UK' || l.Country != 'United Kingdom')){
            Task taskcreate = new Task();
            taskcreate.OwnerId = '00550000005ThR4';
            taskcreate.WhoId = l.id;
            taskcreate.ReminderDateTime = date.today();
            taskcreate.Subject = 'Call';
            taskcreate.Status = 'Completed';
            tsk_lst.add(taskcreate);
        }
    }
    upsert tsk_lst;
}
Thank you for any help. 
 
Akhil ReddyAkhil Reddy
1. Create businesshour variable in test class as you did in your trigger
2. create a case with some created date/time 
3. check if the created date/time is with the business hours rang
4. if so check for the flag on tasks created under that lead