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

Hi @Rekha20 

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response helpful.

Thanks,

Yaswanthi

manjusha_
Kilo Sage

@Rekha20 

 

Check whether gs.dateDiff method works or not in your business rule

If yes then you need to get current date using gs.nowDateTime() method 

 

If your emp_startdate is of type date then you need to split your current date as below-

var date = gs.nowDateTime();//current date
var date1 = date.split(" ");

 

var date2 = date1[0];

var diff = gs.dateDiff(emp_startdate,date2,true);//this gives diff in seconds,you can calculate the diff in days aslo

if diff==0//both dates are same

diff <0//emp start date is greater than current date

diff>0//emp start date is less than current date

 

Mark this answer as correct and helpful if it hepls

Thanks,

Manjusha Bangale