Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

priyatam_pvp
Tera Guru

priyatam_pvp_0-1740408089091.png


The day difference is correct the only thing you need to add is Math.floor at the end to avoid decimal values. If you are looking for a Positive value for the parameters passes exchange the dates while comparison.

Ankur Bawiskar
Tera Patron
Tera Patron

@dvelloriy 

try this, you have used the reverse and hence it's negative

    var daysDifference = GlideDateTime.subtract(currentDate, effectiveDate).getNumericValue() / (1000 * 60 * 60 * 24);
 

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

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

Thanks Ankur. I also see a problem in creating scheduled job. Do you see any anomalies here as well?

When i search the sys_trigger table with job name, it does not show anything. 

 

    var scheduledJob = new GlideRecord('sys_trigger');
    scheduledJob.initialize();
    scheduledJob.name = 'assign to Group B';
    scheduledJob.script = 'var gr = new GlideRecord("' + tableName + '"); if (gr.get("' + triggerRecordSysId + '")) { gr.assignment_group = "' + groupB + '";gr.state=10; gr.update(); }';
    scheduledJob.next_action = effectiveDate.addDaysLocalTime(-30);
    scheduledJob.insert();

@dvelloriy 

it won't be present in scheduled job

the records created in sys_trigger are triggered by system based on next_action date/time field

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

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