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

 

5 REPLIES 5

soumyadeep10
Tera Contributor

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.

No this did not work 

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