- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2018 06:02 AM
Hi All,
I have created a custom table (u_release_schedule) and this contains the fields u_start_date and u_product
I now need to create a schedule job to Automatically run a script of your choosing - I am going to get this to run daily and need to add a condition to check the u_release_schedule tables, u_start_date field and u_product fields/
I want the scheduled job to only run when the product = 'Strata' & current date = u_start_date
Just wondering if anyone had some pointers in regards to comparing the date in the field to the current date?
Any help is greatly appreciated
Thanks
Sam
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2018 02:22 AM
I've just had a look, that will be because gs.nowDateTime() does not have a date property. I was thinking of new GlideDateTime(); the below should work.
var date = new GlideDateTime().date;
var sched = new GlideRecord('u_release_schedule');
sched.addQuery('u_product', 'strata');
sched.addQuery('u_start_date', date);
sched.query();
if (sched.next()) {
var gr = new GlideRecord('rm_release');
gr.initialize();
gr.cmdb_ci = sched.u_configuration_item;
gr.u_release_product = sched.u_product;
gr.u_cdl_scheduled_start_date = sched.getDisplayValue('u_start_date');
gr.u_cdl_scheduled_end_date = sched.getDisplayValue('u_end_date');
gr.insert();
}
In your logs above you will have only got [object GlideRecord] as a result for sched in the first log, this is because it's still looking at the query, when logging you should log the properties directly or else it will always come back with that.
If you search for 'Scripts - Background' in the nav pane at the left you are able to run scripts and log anything you need using gs.print directly to the page, however, background scripts do run the full script so if you use that module make sure that you're not making full changes to the system that may cause system issues.
There is also a handy addon called Xplore which allows you to look at the properties and subfunctions for further debugging.
Kind Regards
Links:
Background Scripts — ServiceNow Elite
Be a Rockstar Developer with Xplore: Developer Toolkit

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2018 07:00 AM
Please use below as sample scheduled job script
var tbl = new GlideRecord('u_release_schedule');
tbl.addQuery('u_product',"Strata");
tbl.addQuery('u_start_date','>',gs.nowDateTime());
tbl.query();
while(tbl.next()){
// add your logic
}
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2018 07:57 AM
HI Sachin,
Thanks for the above.
Do I need to add anything else to the above for it to run the script in the 'Run this script' section?
Thanks
Sam

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2018 08:16 AM
you will have to your processing logic in while loop if you want
var tbl = new GlideRecord('u_release_schedule');
tbl.addQuery('u_product',"Strata");
tbl.addQuery('u_start_date','>',gs.nowDateTime());
tbl.query();
while(tbl.next()){
// add your logic
}
Regards,
sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2018 08:06 AM
Hi Sam,
I would go with
var gr= new GlideRecord('u_release_schedule');
gr.addQuery('u_product',"Strata");
gr.addQuery('u_start_date',gs.nowDateTime().date);
gr.query();
if (gr.next()) {
return true;
}
else {
return false;
}
The conditions look for a true or false value so you will need to return these. If you're looking for them to run on the same day that is in the new table you would need to make sure it's equal in the query as well, the above should do that but dealing with specific dates can be tricky so it will be worth testing it first.
Kind Regards