You need to sign in to do that
Don't have an account?
Craig 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:
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?
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?
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
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
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?
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
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 :)