//Trigger to compare business hours with specific number of hours in apex trigger and update the case status value
Trigger Test_Business_hours_Trigger on emailmessage (before insert,after insert,before update,after update) {
// case cas {get;set;}
long l;
//Trigger to compare business hours with specific number of hours in apex trigger and update the case status value
Trigger Test_Business_hours_Trigger on emailmessage (before insert,after insert,before update,after update) {
// case cas {get;set;}
long l;
Case cas = [SELECT ClosedDate, status FROM Case WHERE ClosedDate != NULL LIMIT 1];
//SOQL query to get the business hours defined in the org
BusinessHours bh =
[Select
id,SundayStartTime, MondayStartTime, TuesdayStartTime,
WednesdayStartTime, ThursdayStartTime, FridayStartTime,
SaturdayStartTime, SundayEndTime, MondayEndTime,TuesdayEndTime,
WednesdayEndTime, ThursdayEndTime, FridayEndTime,SaturdayEndTime
From BusinessHours
Where IsDefault=true];
//gets closed date and system.now() in to the variables to define in business hours bh.diff() method
DateTime startdate = cas.ClosedDate ;
DateTime systemnow = system.now();
//SOQL query to get the business hours defined in the org
BusinessHours bh =
[Select
id,SundayStartTime, MondayStartTime, TuesdayStartTime,
WednesdayStartTime, ThursdayStartTime, FridayStartTime,
SaturdayStartTime, SundayEndTime, MondayEndTime,TuesdayEndTime,
WednesdayEndTime, ThursdayEndTime, FridayEndTime,SaturdayEndTime
From BusinessHours
Where IsDefault=true];
//gets closed date and system.now() in to the variables to define in business hours bh.diff() method
DateTime startdate = cas.ClosedDate ;
DateTime systemnow = system.now();
long startl = startdate.getTime();
long nowl = systemnow.getTime();
// assigning the number of hours(business hours) i.e, the difference between the startdate and systemnow in to variable 'l'
l = businesshours.diff(bh.id,startdate,systemnow);
system.debug(l);
//For trigger.new values if status is closed and number of business hours are less than 48 hours(172800000 milliseconds) then updates the status to working
For(Emailmessage em : trigger.new){
if((Cas.Status=='Closed') && (l<172800000)) {
cas.status='Working';
update Cas;
}
}
}
// assigning the number of hours(business hours) i.e, the difference between the startdate and systemnow in to variable 'l'
l = businesshours.diff(bh.id,startdate,systemnow);
system.debug(l);
//For trigger.new values if status is closed and number of business hours are less than 48 hours(172800000 milliseconds) then updates the status to working
For(Emailmessage em : trigger.new){
if((Cas.Status=='Closed') && (l<172800000)) {
cas.status='Working';
update Cas;
}
}
}