Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2025 01:35 AM
Hi,
I am trying to get the difference between two dates into days in a scoped application but somehow it's not working below is the code.
(function executeRule() {
var gr = new GlideRecord('CUSTOM TABLE');
gr.query();
var rowCount = gr.getRowCount();
var count = 0;
while (gr.next()) {
count++;
var nowGdt = new GlideDateTime();
var createdGdt = gr.sys_created_on;
gs.info("get Dates: "+ nowGdt +" " + createdGdt);
var diff = GlideDateTime.subtract(nowGdt, createdGdt);
gs.info("get Diff: "+ diff);
var dur = diff.getNumericValue();
gs.info("getdiff: "+ dur);
//var diffMs = nowGdt.getNumericValue() - createdGdt.getNumericValue();
var elapsedDays = dur / (1000 * 60 * 60 * 24);
var aging = '';
if (elapsedDays <= 2) aging = '0-2 Days';
else if (elapsedDays <= 7) aging = '3-7 Days';
else if (elapsedDays <= 14) aging = '8-14 Days';
else if (elapsedDays <= 21) aging = '15-21 Days';
else if (elapsedDays <= 28) aging = '22-28 Days';
else aging = '> 28 Days';
if (aging) {
gs.info('[AGE] ' + gr.number + ' → ' + aging);
} else {
gs.info('[WARN] ' + gr.number + ' → ' + 'NO ageing value assigned elasped days:' + elapsedDays);
}
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.ageing_category = aging;
gr.update();
}
})();
Please let me know if something is missing in code
Solved! Go to Solution.
1 ACCEPTED SOLUTION
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2025 01:55 AM
some corrections
try this
(function executeRule() {
var gr = new GlideRecord('CUSTOM TABLE');
gr.query();
while (gr.next()) {
var nowGdt = new GlideDateTime();
// Create GlideDateTime object from sys_created_on string
var createdGdt = new GlideDateTime(gr.sys_created_on.toString());
// Subtract createdGdt from nowGdt to get GlideDuration
var diff = GlideDateTime.subtract(createdGdt, nowGdt);
gs.info("get Diff: " + diff);
// diff is a GlideDuration; get numeric value in milliseconds
var dur = diff.getNumericValue();
gs.info("getdiff (ms): " + dur);
// Convert milliseconds to days
var elapsedDays = dur / (1000 * 60 * 60 * 24);
gs.info("Elapsed days: " + elapsedDays);
var aging = '';
if (elapsedDays <= 2)
aging = '0-2 Days';
else if (elapsedDays <= 7)
aging = '3-7 Days';
else if (elapsedDays <= 14)
aging = '8-14 Days';
else if (elapsedDays <= 21)
aging = '15-21 Days';
else if (elapsedDays <= 28)
aging = '22-28 Days';
else
aging = '> 28 Days';
gs.info('[AGE] ' + gr.getValue('number') + ' → ' + aging);
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.ageing_category = aging;
gr.update();
}
})();
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2025 01:55 AM
some corrections
try this
(function executeRule() {
var gr = new GlideRecord('CUSTOM TABLE');
gr.query();
while (gr.next()) {
var nowGdt = new GlideDateTime();
// Create GlideDateTime object from sys_created_on string
var createdGdt = new GlideDateTime(gr.sys_created_on.toString());
// Subtract createdGdt from nowGdt to get GlideDuration
var diff = GlideDateTime.subtract(createdGdt, nowGdt);
gs.info("get Diff: " + diff);
// diff is a GlideDuration; get numeric value in milliseconds
var dur = diff.getNumericValue();
gs.info("getdiff (ms): " + dur);
// Convert milliseconds to days
var elapsedDays = dur / (1000 * 60 * 60 * 24);
gs.info("Elapsed days: " + elapsedDays);
var aging = '';
if (elapsedDays <= 2)
aging = '0-2 Days';
else if (elapsedDays <= 7)
aging = '3-7 Days';
else if (elapsedDays <= 14)
aging = '8-14 Days';
else if (elapsedDays <= 21)
aging = '15-21 Days';
else if (elapsedDays <= 28)
aging = '22-28 Days';
else
aging = '> 28 Days';
gs.info('[AGE] ' + gr.getValue('number') + ' → ' + aging);
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.ageing_category = aging;
gr.update();
}
})();
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader