Scheduled Script execution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hello everyone I am trying to create a scheduled job:
A scheduled job should run and check the end date within the Forescout Pen Test table.
If the end date has passed, the job should set the records to Disposed.
this is my script:
But the status is not updating to disposed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @LJ_23 ,
From what I can see, your u_end_date field is a date field while in the script while querying the field you have used the gs.nowDateTime() function.
Can you replace it with gs.now() and check whether it works or not.
*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
17m ago
No this did not work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
try this in background script, if that works there then it works in Scheduled job as well.
-> Remove extra spaces inside addQuery operators
// Scheduled job to dispose pen test records past their end date
var gr = new GlideRecord('u_penetration_testing');
// Query records where end date is on or before now AND status is not 'Disposed'
gr.addQuery('u_end_date', '<=', gs.nowDateTime());
gr.addQuery('u_status', '!=', 'Disposed');
gr.query();
while (gr.next()) {
gr.u_status = 'Disposed';
gr.update();
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
try to form the filter condition and then copy the query and use in encodedQuery by picking it using Right Click
// Scheduled job to dispose pen test records past their end date
var gr = new GlideRecord('u_penetration_testing');
// Query records where end date is on or before now AND status is not 'Disposed'
var encodedQuery = 'review_date<javascript:gs.beginningOfToday()^state!=-4';
gr.addEncodedQuery(encodedQuery);
gr.query();
while (gr.next()) {
gr.u_status = 'Disposed';
gr.update();
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
