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
Craig PhoenixCraig Phoenix 

Set timeframe for trigger to run

I have created a trigger to run before insert to send an alert when cases reach 5 within 60 minutes of the most recent case.

Code:
trigger OutageAlert on Case (before insert) {
    date today =system.today();
    List<Case> numberofcases = [SELECT ID FROM CASE where SLA__c<=:1];
        for(case cas:trigger.new){
            if(cas.origin == 'Web - Email'){
                if(numberofcases.size() == 5) {
                    cas.SendAlert__c = true;
                }
            }
        }
}

I am wondering now how I can have this code ONLY run outside of business hours? for example how could I have this only perform the action to mark SendAlert__c = True IF it is between 17:00-06:00 Monday to Friday and all day/night Saturday and Sunday?

Is it possible to have it limited to only occur during none business times?
Best Answer chosen by Craig Phoenix
Maharajan CMaharajan C
Hi Craig,

Try with Business Hours. 

Create the business hour in your Org from Setup > Business Hours > create new > but set the business hour as 17:00-06:00 Monday to Friday and for Saturday and Sunday use 24 Hours.

Then in Trigger use the Business Hours Class.

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name=''];   // or use the ID in Where Condition
Boolean isWithin= BusinessHours.isWithin(bh.id, System.Now() );

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm


Trigger should be like below:

trigger OutageAlert on Case (before insert) {
   DateTime now =system.Now();
   BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name=''];   // or use the ID in Where Condition
   Boolean isWithin= BusinessHours.isWithin(bh.id,
now);   
   if(isWithin){
   
List<Case> numberofcases = [SELECT ID FROM CASE where SLA__c<=:1];
        for(case cas:trigger.new){
            if(cas.origin == 'Web - Email'){
                if(numberofcases.size() == 5) {
                    cas.SendAlert__c = true;
                }
            }
        }
}
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Craig,

Try with Business Hours. 

Create the business hour in your Org from Setup > Business Hours > create new > but set the business hour as 17:00-06:00 Monday to Friday and for Saturday and Sunday use 24 Hours.

Then in Trigger use the Business Hours Class.

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name=''];   // or use the ID in Where Condition
Boolean isWithin= BusinessHours.isWithin(bh.id, System.Now() );

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm


Trigger should be like below:

trigger OutageAlert on Case (before insert) {
   DateTime now =system.Now();
   BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name=''];   // or use the ID in Where Condition
   Boolean isWithin= BusinessHours.isWithin(bh.id,
now);   
   if(isWithin){
   
List<Case> numberofcases = [SELECT ID FROM CASE where SLA__c<=:1];
        for(case cas:trigger.new){
            if(cas.origin == 'Web - Email'){
                if(numberofcases.size() == 5) {
                    cas.SendAlert__c = true;
                }
            }
        }
}
}

Thanks,
Maharajan.C
This was selected as the best answer
Craig PhoenixCraig Phoenix
Hi Maharajan,

When trying to create the business hours it states "Error: Start time must be earlier than end time" when trying to enter the start time as 5 p.m. and the end time as 6 a.m.

Is there a way to use the correct business hours but tell it to only trigger if NOT within the business hours? I created a business hours with the name of "Support Business Hours" that is 06:00 to 17:00 Monday through Friday, is it possible to trigger only if the current system time is not within that business hours?
Maharajan CMaharajan C
Yes Craig, We can use like below:

And also don't enable the business hours for Saturday and Sunday. .Clear the Values and disable the 24hours in Saturday and Sunday it should be No Hours.

trigger OutageAlert on Case (before insert) {
   DateTime now =system.Now();
   BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name=''];   // or use the ID in Where Condition
   Boolean isWithin= BusinessHours.isWithin(bh.id, now);   
   if(!isWithin){     // use the not (!) in if condition
   List<Case> numberofcases = [SELECT ID FROM CASE where SLA__c<=:1];
        for(case cas:trigger.new){
            if(cas.origin == 'Web - Email'){
                if(numberofcases.size() == 5) {
                    cas.SendAlert__c = true;
                }
            }
        }
}
}

Thanks,
Maharajan.C
Craig PhoenixCraig Phoenix
Thanks Maharajan for setting me down the right path, I made some adjustments and found a way to get it to work outside of business hours using the ELSE statement.
 
trigger OutageAlert on Case (before insert) {
   DateTime now =system.Now();
   BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
   Boolean isWithin= BusinessHours.isWithin(bh.id, now);   
   if(isWithin){
    Return;
   }else{
       List<Case> numberofcases = [SELECT ID FROM CASE where CaseAge__c<=:1];
        for(case cas:trigger.new){
            if(cas.origin == 'Web Email'){
                if(numberofcases.size() == 10) {
                    cas.SendAlert__c = true;
                }
                if(numberofcases.size() == 25) {
                    cas.SendAlert25__c = true;
                }
            }
        }
}
}

As you can see instead of the list after if(iswithink) I did a simple Return; then used }else{ and placed the code there so that when it checks the business IF it is within the hours it simply returns and continues as normal however IF it is not within business hours it now executes the trigger code.

Thanks for the assistance :)