Need to close case when employment start date passed one day.

Rekha20
Tera Contributor

Hi All,

 

I need to auto close a case when subject person's employment start date crosses one day. I have written below logic on before/update BR:

 

(function executeRule(current, previous /*null when async*/) {
    var profileGr = new GlideRecord('sn_hr_core_profile');
    profileGr.addQuery('user', current.subject_person);
    profileGr.query();
 
    if (profileGr.next()) {
        var employmentStartDate = profileGr.employment_start_date; 
        var employmentEndDate = profileGr.employment_end_date;
 
        if (employmentStartDate) {
            // Check if the employment start date is exactly 91 days ago
            var currentDate = new GlideDateTime();
gs.addInfoMessage("currentDate: " + currentDate);
 
var startDateTime = new GlideDateTime(employmentStartDate);
gs.addInfoMessage("startDateTime: " + startDateTime);
 
var daysSinceStart = startDateTime.getNumericValue() - currentDate.getNumericValue();
gs.addInfoMessage("daysSinceStart: " + daysSinceStart);
 
            if (daysSinceStart > 1) {
                current.state = 3;  //Closed Complete
            }
        }
 
       
    }
 
 
})(current, previous);
  
Logs are coming as: 
currentDate: 2023-11-17 06:44:38
startDateTime: 2023-11-15 00:00:00
daysSinceStart: -197078123
 
Current date and start date time is correct but daysSinceStart is not right. Also, employment_start_date field is date type field not date/time. Please help to correct this code.
1 ACCEPTED SOLUTION

Hi @Rekha20 

change the steps like below and try

 var currentDate = new GlideDate();
gs.addInfoMessage("currentDate: " + currentDate);

var startDateTime = new GlideDateTime(employmentStartDate);
var startDate= startDateTime.getLocalDate();
gs.addInfoMessage("startDateTime: " + startDate);
var dateDifferenceInMs = startDate.getNumericValue() - currentDate.getNumericValue();
var dateDifferenceInDays =Math.ceil( dateDifferenceInMs / 24/ 60 / 60 / 1000);
gs.print(dateDifferenceInDays);

 

View solution in original post

11 REPLIES 11

yaswanthi2
Giga Sage

Hi @Rekha20 

use date difference like below

var daysSinceStart =gs.dateDiff(startDateTime.getDisplayValue(), currentDate.getDisplayValue(), false);

 

Hi @yaswanthi2 

Thanks for response. as this is in scoped application so getting error like: Function dateDiff is not allowed in scope sn_hr_le. Use GlideDateTime.subtract() instead

You can use below code for date subtract in scoped application

var daysSinceStart= GlideDateTime.subtract(startDateTime.getDisplayValue(), currentDate.getDisplayValue());
gs.info(daysSinceStart.getDisplayValue());

Rekha20
Tera Contributor

HI @yaswanthi2 

 

Now getting error : exception: org.mozilla.javascript.EvaluatorException: Can't find method com.glide.script.fencing.ScopedGlideDateTime.subtract(string,string).