- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 10:44 AM
Hi,
So with a little help from the community I am almost in a good position.
What I need is for the schedule job to run daily. It needs to search the u_infra_certificates table and query the u_expiry_date.
If this date is EXACTLY 30 days from today it will trigger an Incident.
The incident part works fine but what I cannot get it to do is only run it if it is exactly 30 days.
So when it runs daily it skips it if the date is 29 days or 31 days.
My script is below.
var rec = new GlideRecord('u_infra_certificates');
rec.query();
if(rec.next()) {
var now = new GlideDateTime();
var gdt = new GlideDateTime(''+rec.u_cert_expiry_date);
var diffSeconds = gs.dateDiff(gdt, now, true);
if (diffSeconds < 2592000) {
//creates incident
var gr = new GlideRecord('incident');
gr.intialize();
gr.short_description = 'Certificate renewal required';
gr.assignment_group = '5e8550e90f6e3900f6e783fc22050ef3';
gr.description = rec.u_subject_name;
//gr.cmdb_ci = 'Certificate Services (Corporate Internal)';
gr.setDisplayValue('cmdb_ci','Certificate Services (Corporate Internal)');
//gr.u_inf_certificate = current.number;
gr.u_inf_certificate = rec.sys_id;
gr.insert();
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 01:03 PM
Your dates are in the future. I was under the impression that you wanted to create an incident if it had already expired. Try using positive numbers instead of negative numbers. I also discovered that there is a GlideDate method that was used since I created the original script, so there is no need to strip off the time:
var chkDate = new GlideDate();
chkDate.addDays(30);
var rec = new GlideRecord('u_infra_certificates');
rec.addQuery('u_cert_expiry_date', chkDate);
rec.query();
gs.print(rec.getRowCount());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 11:04 AM
What I have done for similar situations is to use the date parameters as part of the query string so that I only get those that I want. Here is a portion of a script I use to check if a knowledge article is about to expire. It will find those records in which the valid_to date is less than or equal to 7 days from today. Then it captures the author for use later on in the script:
function checkRevDate() {
var aDt = new GlideDateTime();
aDt.addDays(7);
var cDate = aDt.toString().split(' ')[0];
var kb = new GlideRecord('kb_knowledge');
kb.addQuery('workflow_state', '!=', 'retired');
kb.addQuery('author', '!=', '');
kb.addQuery('valid_to', '<=', cDate);
kb.query();
kbArray = [];
while (kb.next()) {
kbArray.push(kb.author.toString());
}
//removed remainder
}
Feel free to adjust to fit your scheduled job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 11:11 AM
Hi Christopher,
Thanks this is helpful however I may need a little more help. I am not really a scripter.
To start with, am I starting my code the right way. It creates the Incident however I find that it just picks the first record on the table and creates the Incident on that. It does not query anything.
Secondly, On your above script, where are you referring to the table?
Thanks,
Riaz

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 11:35 AM
I have modified your script accordingly. I took the liberty of removing your commented lines as well as change the gr object to be incObj to make the code more self documenting. This should find only those records that have the u_cert_expiry_date set at 30 days ago:
var aDt = new GlideDateTime();
aDt.addDays(-30);
var cDate = aDt.toString().split(' ')[0];
var rec = new GlideRecord('u_infra_certificates');
rec.addQuery('rec.u_cert_expiry_date', cDate);
rec.query();
while(rec.next()) {
//creates incident
var incObj = new GlideRecord('incident');
incObj.intialize();
incObj.short_description = 'Certificate renewal required';
incObj.assignment_group = '5e8550e90f6e3900f6e783fc22050ef3';
incObj.description = rec.u_subject_name;
incObj.setDisplayValue('cmdb_ci','Certificate Services (Corporate Internal)');
incObj.u_inf_certificate = rec.sys_id;
incObj.insert();
}
Let me know if you have further questions,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 11:47 AM
Thank you Christoper, much appreciated.
While it creates the Incidents (I have about 10 for testing) it creates an Incident for each record.
I replaced the (-30) with a (-1) just to test but still get the same result of all incidents being created.
Is it checking the date field?
Thanks,
Riaz