## Monday, December 24, 2018

### Calculate Number of Working Days And Business Hours A Case Has Been Opened

This solution aims to count the number of working days and business hours a case has been opened.

Note the following:

• Working days are equivalent to Mondays to Fridays (excluding weekend).
• Business hours from 8:00 AM to 6:00 PM local time (NetSuite User Preference)
• If the case is not yet closed, the result will be relative to today's date and time.
• The result will be in this format:  X Days Y Hours Z Mins.

1.    Navigate to List > Search > Saved Searches > New.

2.    Click Case.

3.    Enter a Search Title.

4.    Under Results subtab, add the following fields:

- Number

- Formula (Text) <insert the formula below in the Formula column>

CASE WHEN {closed} IS NULL THEN decode(((TRUNC({now},'D')-TRUNC({createddate}+6,'D'))/7*5)+MOD(7-TO_CHAR({createddate},'D'),6)+LEAST(TO_CHAR({now},'D')-2,5)- 1,-1,0,((TRUNC({now},'D')-TRUNC({createddate}+6,'D'))/7*5)+MOD(7-TO_CHAR({createddate},'D'),6)+LEAST(TO_CHAR({now},'D')-2,5)-1)+ decode(trunc({createddate}),trunc({now}),0,trunc(((decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 17-to_char({createddate},'hh24') when to_char({createddate},'hh24')<8 then 10 else 0 end)+decode(to_char({now},'D'),1,0,7,0,case when to_char({now},'hh24') between 8 and 18 then to_char({now},'hh24')-8 when to_char({now},'hh24')<8 then 0 else 10 end))+trunc((decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 60-to_char({createddate},'mi') else 0 end)+decode(to_char({now},'D'),1,0,7,0,case when to_char({now},'hh24') between 8 and 17 then to_number(to_char({now},'mi')) else 0 end))/60))/10))||' days '||mod(decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 17-to_char({createddate},'hh24') when to_char({createddate},'hh24')<8 then 10 else 0 end)+decode(to_char({now},'D'),1,0,7,0,case when to_char({now},'hh24') between 8 and 18 then to_char({now},'hh24')-8 when to_char({now},'hh24')<8 then 0 else 10 end)+trunc((decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 60-to_char({createddate},'mi') else 0 end)+decode(to_char({now},'D'),1,0,7,0,case when to_char({now},'hh24') between 8 and 17 then to_number(to_char({now},'mi')) else 0 end))/60),10)||' hours '||mod((decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 60-to_char({createddate},'mi') else 0 end)+decode(to_char({now},'D'),1,0,7,0,case when to_char({now},'hh24') between 8 and 17 then to_number(to_char({now},'mi')) else 0 end)),60)||' mins ' ELSE decode(((TRUNC({closed},'D')-TRUNC({createddate}+6,'D'))/7*5)+MOD(7-TO_CHAR({createddate},'D'),6)+LEAST(TO_CHAR({closed},'D')-2,5)- 1,-1,0,((TRUNC({closed},'D')-TRUNC({createddate}+6,'D'))/7*5)+MOD(7-TO_CHAR({createddate},'D'),6)+LEAST(TO_CHAR({closed},'D')-2,5)-1)+ decode(trunc({createddate}),trunc({closed}),0,trunc(((decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 17-to_char({createddate},'hh24') when to_char({createddate},'hh24')<8 then 10 else 0 end)+decode(to_char({closed},'D'),1,0,7,0,case when to_char({closed},'hh24') between 8 and 18 then to_char({closed},'hh24')-8 when to_char({closed},'hh24')<8 then 0 else 10 end))+trunc((decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 60-to_char({createddate},'mi') else 0 end)+decode(to_char({closed},'D'),1,0,7,0,case when to_char({closed},'hh24') between 8 and 17 then to_number(to_char({closed},'mi')) else 0 end))/60))/10))||' days '||mod(decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 17-to_char({createddate},'hh24') when to_char({createddate},'hh24')<8 then 10 else 0 end)+decode(to_char({closed},'D'),1,0,7,0,case when to_char({closed},'hh24') between 8 and 18 then to_char({closed},'hh24')-8 when to_char({closed},'hh24')<8 then 0 else 10 end)+trunc((decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 60-to_char({createddate},'mi') else 0 end)+decode(to_char({closed},'D'),1,0,7,0,case when to_char({closed},'hh24') between 8 and 17 then to_number(to_char({closed},'mi')) else 0 end))/60),10)||' hours '||mod((decode(to_char({createddate},'D'),1,0,7,0,case when to_char({createddate},'hh24') between 8 and 17 then 60-to_char({createddate},'mi') else 0 end)+decode(to_char({closed},'D'),1,0,7,0,case when to_char({closed},'hh24') between 8 and 17 then to_number(to_char({closed},'mi')) else 0 end)),60)||' mins ' END

5.  Click Save & Run.

DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.

Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.

Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.