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
Ravi NarayananRavi Narayanan 

calculating case age in a Date/Time Custom Field

Hi Guys,

 

I have a scenario. Please someone help me ! 

I want a CUSTOM Created Date Time  Field in CASE OBJECT. 

This Field should have values based on Working Hours . 

Monday to Thursday - 8 AM to 4 PM 

Friday - 8 AM to 3 PM 

SATURDAY and SUNDAY - HOLIDAY 


FOR EXAMPLE :Case created by 4:01 PM on Monday, the value of Custom Created date field should be TUESDAY 8 AM .
If a Case is created by 3:01 PM on friDAY, the value of Custom field should be MONDAY 8 AM . 

Please help me with this scenario  !  



Best Answer chosen by Admin (Salesforce Developers) 
k_bentsenk_bentsen

1. You'll need to set up business hours in the Company Profile > Business Hours section of setup. Salesforce provides you with a default set of 24/7 which you can adjust to fit the times you've mentioned. Salesforce automatically assigns all new cases to these business hours, unless you have built case assignment rules with additional business hours, which you shouldn't need to worry about.

 

2. Write a trigger on insert that looks at the current time with System.now() and checks to see if it's within the cases business hours by using the BusinessHours static method isWithin. If the value is returned false, use the static method nextStartDate to set your custom field. If it's true then just set your field to the System.now() value, assuming of course that is behavior you're looking for.

 

 

 

 

All Answers

k_bentsenk_bentsen

1. You'll need to set up business hours in the Company Profile > Business Hours section of setup. Salesforce provides you with a default set of 24/7 which you can adjust to fit the times you've mentioned. Salesforce automatically assigns all new cases to these business hours, unless you have built case assignment rules with additional business hours, which you shouldn't need to worry about.

 

2. Write a trigger on insert that looks at the current time with System.now() and checks to see if it's within the cases business hours by using the BusinessHours static method isWithin. If the value is returned false, use the static method nextStartDate to set your custom field. If it's true then just set your field to the System.now() value, assuming of course that is behavior you're looking for.

 

 

 

 

This was selected as the best answer
Ravi NarayananRavi Narayanan
Hi.. Thanks for your reply.. i will try it now .

BTW, Do you mean all new cases will be automatically assignedd to these business hours ? .. can u elaborate please !
k_bentsenk_bentsen

That's correct. All new case objects are assigned to your organization's default business hours. Again, Salesforce automatically sets up the default business hours as 24/7 for you. You can create additional business hours and use assignment rules to certain cases based on criteria. If a case doesn't fit any of your criteria for assignment, then it is assigned to the default business hours.

 

For example, our org uses rules to assign business hours based on location. So if a case comes into one of the UK team queues, the case will be assigned business hours that are in-line with the European work day. The same behavior applies for cases for the US and Australia team queues.

Ravi NarayananRavi Narayanan

i am getting a error

Method does not exist or incorrect signature: BusinessHours.nextStartDate(Id, Datetime)"

for(case con:trigger.new)
{
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime targetTime = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
Datetime nextStart = BusinessHours.nextStartDate(bh.id, targetTime);
con.test2__c=nextStartDate;
}

 

k_bentsenk_bentsen

I guess you need to cast the id into into String:

 

Datetime nextStart = BusinessHours.nextStartDate((String)bh.id, targetTime);

 

Seems like Salesforce has an error on the businesshours static methods description page.

Ravi NarayananRavi Narayanan

Hi ,

 

isWithin and nextStartDate seems to be winter 14 release . 



P5P5
@k_bentsen, @Ravi Narayanan - Could you please share the trigger logic ?

Looking to maintain the Reporting for the Milestones for SLA (Service Level Agreement) calculations. We're looking to implement the batch interface to run once nightly and calculate overal Case "Age" in business hours and relevant "Pending" With Times in business hours.

Could anyone please suggest the way to go forward ?