Schedule Job Condition to check start date in a custom table with current date

Sam Ogden
Tera Guru

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

1 ACCEPTED SOLUTION

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


View solution in original post

16 REPLIES 16

Hi Chris,



Thanks for the above.   When I try and save this I am getting an error:



find_real_file.png



Thanks


My bad, I should have checked that before, I have wrapped it in a function and it's now being allowed



checkSched();




function checkSched() {




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;


}


}



I would also say if you're generating a new record, keep the scripting to a minimum, if you select 'Automatically generate something' rather than run a script of your choosing you can create a template which will be created once the conditions are met, this should be the better option unless you need to pipe anything in through the script.


find_real_file.png



Kind Regards


Hi Chris,



thanks for that, now managed to save this.   A template wouldn't work as we the fields will not be the same everytime.



I've written the below, but not sure if it is correct:



find_real_file.png


If it needs to be script I would remove the condition, it's not best practise to run the script every day regardless but it will work, just have the below:



var sched = new GlideRecord('u_release_schedule');


sched.addQuery('u_product', 'strata');


sched.addQuery('u_start_date', gs.nowDateTime().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();


}



I've changed the way the start and end date are added as I have found setting dates through scripts to difficult in the past. Setting them directly to the display value usually resolves the issue.



I would suggest setting a release for today and giving that a run, it should work fine from what I can see.



Kind Regards


HI Chris


Thanks once again for the above.



Not quite sure I understand your point about running this everyday and removing the condition.


the table u_release_schedule will hold all our release for our strata product for the next few years.   I need the scheduled job to check this table for the u_start_date field and then if that matches todays date then run the script and create the new release record.


When the release record is created then it will start a workflow and create multiple tasks etc.



Without the condition in place how would this work? In your example above would this be ran daily?   I was planning on running this out of hours as to avoid any system issues, although to start with this will only create 1 release log roughly once every 6 weeks.   In the future we are likely to have more products following a similar system but this would never lead to a lot of release logs being created at once with the script



Thanks



Sam