Create incident from scheduled job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2017 04:38 AM
Hi
yesterday Chuck started me ok with this script:
var rec = new GlideRecord('u_infrastructure_certificates');
rec.query();
while (rec.next()) {
var now = new GlideDateTime();
var gdt = new GlideDateTime();
gdt.setValue(rec.getValue('u_cert_expiry_date'));
gdt.addWeeksLocalTime(-6);
if (gdt.getNumericValue() < now.getNumericValue()) {
// Expires in 6 weeks or less - do something here
}
}
Not being a scripter please help:
a: how do I change this so it only applies it to the record if the date is exactly 30 days
b: How do I get it to add the below so it creates the incident
var gr = new GlideRecord('incident');
gr.intialize();
gr.short_description = 'Certificate renewal required';
gr.assignment_group = '5e8550e90f6e3900f6e783fc22050ef3';
gr.description = current.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 = current.sys_id;
gr.insert();
Help much appreciated
Thanks,
Riaz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2017 04:42 AM
Hi Riaz,
If you have only one such incident open at any given point of time, you can use the Short Description ==='Certificate renewal required' and active==true condition.
Alternatively, you can have a system property which will contain the sys_id of the created incident and every month the scheduled job will update the sys_id value of the system property whenever the job runs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2017 04:45 AM
Hiya - sorry but you didn't really answer my question

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2017 04:52 AM
Hi Riaz,
Try this. The secret is using GlideDateTime()'s subtract method and getting a duration which you can then use the getDayPart(). I haven't tested this so I don't know if it returns a number or string. You'll need to check to verify, but this is the general idea.
var rec = new GlideRecord('u_infrastructure_certificates');
rec.query();
while (rec.next()) {
var now = new GlideDateTime();
var gdt = new GlideDateTime();
var dur = rec.subtract(rec.u_cert_expiry_date');
if (dur.getDayPart() == '30') {
// Expires in 30 days
var gr = new GlideRecord('incident');
gr.intialize();
gr.short_description = 'Certificate renewal required';
gr.assignment_group = '5e8550e90f6e3900f6e783fc22050ef3';
gr.description = current.u_subject_name;
gr.cmdb_ci.setDisplayValue('Certificate Services (Corporate Internal)');
//gr.u_inf_certificate = current.number;
gr.u_inf_certificate = current.sys_id;
gr.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2017 04:58 AM