Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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

 Hi @Rekha20 add like this

Hi @yaswanthi2  Now calculating correct: 

currentDate: 2023-11-17 07:55:00
startDateTime: 2023-11-15 00:00:00 
This is the differnec: 2 Days 7 Hours 55 Minutes
 
Can you please help me to get current date and difference only in Date format not time? as employment_start_date is date field only.

Hi @Rekha20 

change in your script for current date and date substract as like below

var currentDate = new GlideDate();

var daysSinceStart= new GlideDate.subtract(startDateTime, currentDate);

 

Hi @yaswanthi2 

Here is the updated code:

 

(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; 
gs.addInfoMessage("startDateTime: " + employmentStartDate);
        var employmentEndDate = profileGr.employment_end_date;
 
        if (employmentStartDate) {
            // Check if the employment start date is exactly 91 days ago
var currentDate = new GlideDate();
gs.addInfoMessage("currentDate: " + currentDate);
 
var startDateTime = new GlideDateTime(employmentStartDate);
//gs.addInfoMessage("startDateTime: " + startDateTime);
 
var daysSinceStart= new GlideDate.subtract(employmentStartDate, currentDate);
            gs.addInfoMessage(daysSinceStart.getDisplayValue());
 
 
            if (daysSinceStart > 1) {
                current.state = 3;  //Closed Complete
            }
        }
 
       
    }
 
Now when just using GlideDate, getting error: Error running business rule 'Autoclose case on day 91 or less than 90' on sn_hr_le_case: HRC0030171, exception: org.mozilla.javascript.EvaluatorException: Can't find method com.glide.glideobject.GlideDateTime.subtract(com.glide.script.glide_elements.GlideElementGlideObject,com.glide.script.fencing.ScopedGlideDate). (sys_script.ce1737511baa395077be0fa4cc4bcb22.script; line 21)
 
})(current, previous);
 

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);