Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scheduled Script execution

LJ_23
Tera Contributor

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: 

LJ_23_1-1764080634759.png

 

But the status is not updating to disposed?

LJ_23_0-1764080585623.png

 

10 REPLIES 10

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

@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.

@LJ_23 

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

AnkurBawiskar_0-1764090827515.png

 

AnkurBawiskar_1-1764090827605.png

 

 

// 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&colon;gs.beginningOfToday()^state!=-4';

gr.addEncodedQuery(encodedQuery);
gr.query();

while (gr.next()) {
    gr.u_status = 'Disposed';
    gr.update();
}
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@LJ_23 

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! 🙏

 

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

Ankur Bawiskar
Tera Patron
Tera Patron

@LJ_23 

try to form the filter condition and then copy the query and use in encodedQuery by picking it using Right Click

AnkurBawiskar_1-1764081560241.png

AnkurBawiskar_2-1764081579532.png

 

// 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&colon;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! 🙏

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