- 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-05-2018 01:06 AM
Hi Sam,
With the script above, this will only create the records when the schedule query is matched, so only on the days of the Strata release will any new releases be created. The script will always run on the specific time you set now that you have the criteria in the script instead if that makes sense.
You can set it for 1am every Monday for example and that will work fine as long as you are sure that the release date will always be a Monday, the script just needs to run on a date that is definitely going to hit the release date. When I've had to do this in the past I've always just had the scripts run daily to make sure that the date I've chosen is being hit constantly.
Kind Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2018 02:06 AM
Hi Chris,
Currently have this setup as:
I've tried to use the execute now function but not release record has been produced.
Below is the release schedule data:
I've tried to add some log statements to try and work out where it is going wrong, but not sure I've done these right, any help will be greatly appreciated,
Thanks
Sam
- 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-05-2018 02:31 AM
Hi Chris,
Just tried and it didn't work, but then realised that u_product is a reference field. Once I put the sys_id in place instead of Strata it worked a treat.
Thanks for all your help - apologies if many of the questions are basic still trying to get to grips with coding as no previous experience before SN.
I'll take a look at the links as well
Thanks once again
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2018 02:32 AM
Apologies Chris,
Just one last question - when you mention you should log the properties directly what do you mean exactly? do you have an example just for me to get my head around
Thanks
Sam