Using JavaScript for DateTime field in new Condition Builder
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2023 12:05 AM - edited 04-26-2023 05:46 PM
This issue has already been resolved, but please let me know if you have a better solution.
The requirement was to display only those records from the incident table that were not updated in the last 48 hours, excluding weekends in Report. To exclude weekends, the following script was used:
var gdt = new GlideDateTime();
if ([1, 2].indexOf(gdt.getDayOfWeekUTC()) != -1) { // Mon, Tue
gdt.addDays(-4);
} else if ([7].indexOf(gdt.getDayOfWeekUTC()) != -1) { // Sun
gdt.addDays(-3);
} else {
gdt.addDays(-2);
}
gs.print(gdt);
However, this script could not be used in the DateTime field in the new Condition Builder UI because JavaScript is not allowed by default. To work around, the value was altered before it was stored in AngularJS state by putting a breakpoint in buildFieldValue function and the onChange method call in the web browser developer tool.
Set the breakpoint at the line with onChange call.
Add "Updated" field, "at or before" and click This Month to developer tool to stop executing the script at the breakpoint.
Then in the web browser's console window, the following script was executed to alter the value.
$scope.field.value = "javascript:var gdt = new GlideDateTime(); if ([1, 2].indexOf(gdt.getDayOfWeekUTC()) != -1) { gdt.addDays(-4); } else if ([7].indexOf(gdt.getDayOfWeekUTC()) != -1) { gdt.addDays(-3); } else { gdt.addDays(-2); } gdt;";
Then, click the continue icon in the debugger.
Save the report
Reload the browser page, ta-da! the script is there in the condition as if it is just a normal string filter condition.
I feel it is not favorable workaround but not too risky.
I wish ServiceNow team to provide an empty input box for more dynamic condition.