Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Whats wrong with below code?

dvelloriy
Kilo Sage

Hello All,

 

Whats wrong with below code? I have included this in a flow variable. I am selecting Effective date as Mar 4, 2024. However the datediff is not being calculated correctly. Included a screenshot of logs as well.

 

var triggerRecordSysId = fd_data.trigger.current.sys_id;
var tableName = fd_data.trigger.current.sys_class_name;

var rec = new GlideRecord(tableName);
if(rec.get(triggerRecordSysId)){
var assignment_group = '';
var state = '';
var currentDate = new GlideDateTime();
gs.info("%%%Current Date is"+ currentDate);
 
var effectiveDate = new GlideDateTime(rec.variables.effective_date); // give your variable name here
gs.info("%%%Effective Date is"+ effectiveDate);

// Calculate the difference in days between the current date and the productive date
var daysDifference = GlideDateTime.subtract(effectiveDate, currentDate).getNumericValue() / (1000 * 60 * 60 * 24);
gs.info("%%% Days Diff is"+ daysDifference);
 
dvelloriy_0-1740407491865.png

 

7 REPLIES 7

@ankurbawisk I dont see the record in sys_trigger table. it should atleast show there..

Please note that my flow is in HR core scope. Is this something creating issue ?

@dvelloriy 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

J Siva
Kilo Patron

Hi @dvelloriy 

If you are working on the global scope, then you can use below script.

 

var diff = gs.dateDiff(current Date.getDisplayValue(),effectiveDate.getDisplayValue(),true); //This would return seconds
var days = diff/86400; //Convert the seconds to Days

 

If it's in scoped application, then try below code.

 

var currentDate = gdt.getLocalDate();
var dateDifferenceInMs = currentDate.getNumericValue() - GlideDateTime(effectiveDate).getNumericValue();
var dateDifferenceInDays = Math.floor(dateDifferenceInMs / (1000 * 60 * 60 * 24));
gs.info(dateDifferenceInDays);

 

 

Regards,

Siva