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

Sorry, I forgot to take out a quote. It should be:



var dur = rec.subtract(rec.u_cert_expiry_date);


Thank Chuck,



I tried this but it did not generate an Incident. I have a test record in place which should be in scope of the script to generate an Incident.



find_real_file.png



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() == '1') {


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


      }


}



This is based on a schedule job



Thanks


Riaz


I did a little more digging and found that getDayPart() returns a number. This should do better.



var rec = new GlideRecord('u_infrastructure_certificates');


rec.query();


while (rec.next()) {


  var recStart = rec.getValue('sys_updated_on');


  var now = new GlideDateTime();


  var gdt2 = new GlideDateTime(recStart);


  var dur = GlideDateTime.subtract(gdt2, now); //the difference between gdt1 and gdt2



  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,



Sorry still no go. I replaced the 30 with 1 so it can generate a test incident.



But still not creating the incident



Thank,s


Riaz


Hi Riaz,



I'm afraid I cannot invest any additional time in to writing and debugging your script this morning.



If you are on Istanbul (or higher), I recommend getting familiar with the Script Debugger.


If not, you'll have to start putting in some debug statements to find out what values are being used and what is triggering and not.


http://wiki.servicenow.com/index.php?title=Scoped_Script_Logging