Scheduled Script execution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 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
Hey @LJ_23,
did you see the other answer from Ankur?
Maybe you need to scroll to the bottom of the screen and click "load more replies"
This should be the solution imho.
Cheers
Oli
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
@LJ_23 choices look fine to me. The code looks fine as well, but yeah you still need to change the gs.nowDateTime() to gs.Now() for the code to work as your u_end_date field is a Date field and not a Date/Time field if I'm not wrong.
Also, I would suggest if after the query statement, if you could put a log statment to check the record numbers you're getting after query to verify the query is working fine or not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
please check my below 2 responses and it should work fine.
Ensure there is no before update BR which stops the update if any field is not populated.
Ensure there is no data policy which makes some fields mandatory on Status = Disposed
sharing here again
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();
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 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
5 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
