Create incident from scheduled job

riaz_mansuri
Kilo Guru

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

12 REPLIES 12

Subhajit1
Giga Guru

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.


Hiya - sorry but you didn't really answer my question


Chuck Tomasi
Tera Patron

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();


      }


}


Hi Chuck,



Any idea what the error can be - just trying now.



BTW - I replaced 30 with 1 so I can test



find_real_file.png



Thanks


Riaz