How to send an email from a Script Include with record values.

Wesley Breshear
Tera Expert

Hello,

Can you please assist me with how to perform this action.   I am a very week at JavaScript and basically copying from another script that I got from a developer.   The script basically scans a Certificate table for any records that will be expiring in the next 90 days and then confirms that they are not already in the request system (RITM) to be renewed.   I now want to email the Business Owner of the certificate, with the certificate name, expiration date, brief explanation of they received email and (not necessary but would be nice) a reference link to the record within ServiceNow that is about to expire.

I am not sure if I can just pass scripted values/strings from my 'sc_cat_item' query to email statements within this script, or if I need to go down the path of calling an Event > Email Script > Email Notification?   I don't know how to pass or coordinate the expiring record to an Email Notification if I use that method.   In this case my Business Owner will always be the same email address.  

Any help or guidance would be appreciated!

function ekcmExpireCheck2() {

  gs.info('EKCM: Expiration Check job started.');

  //Get the Certificate catalog items/records

  var itemRecord = new GlideRecord('sc_cat_item');

  itemRecord.addQuery('name', 'Certificate Request');

  itemRecord.query();

  itemRecord.next();

  var Item_sysID = itemRecord.sys_id;

  // Find all 'Active' certs which will expire in the next 3 months

  var ekcmRecord = new GlideRecord('x_teth_ekcm_certificate');

  ekcmRecord.addQuery('install_status', '1');

  ekcmRecord.addEncodedQuery('valid_to<javascript:gs.monthsAgoStart(-3)');

  ekcmRecord.query();

  while (ekcmRecord.next()){

    // Check for existing RITMs against the expiring Certificate list

    var ritmRecord = new GlideRecord('sc_req_item');

    ritmRecord.addQuery('active', 'true');

    ritmRecord.addQuery('cmdb_ci', ekcmRecord.sys_id);

    ritmRecord.query();

  if (!ritmRecord.next()) {

  gs.info('EKCM: Expiration Certificate Email for '+ekcmRecord.name);

          // Send email to Owner, with Cert Name, Expiration date, and Ref Link to expiring record.

   

    }

  }

}

Thank you,

Wesley

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Wesley,



As chuck mentioned this can be achieved via events. You will have to register the event first and then fire the event to trigger notification.


Step 1 : Register Event.


Event Registry - ServiceNow Wiki


Step 2 : Fire event


Events and Email Notification - ServiceNow Wiki


Step 3 : Create email notification


Please refer to the link shared by Chuck above.



Please let us know if you have any questions.


View solution in original post

9 REPLIES 9

Hello Wesley, I think in your case "current" should be ekcmRecord when calling the gs.eventQueue


Hi Siva,



Can you help me with syntax?   I tried following with no success.


gs.eventQueue('xteth_ekcm.cert.expiration.notification', current, 'ekcmRecord');


gs.eventQueue('xteth_ekcm.cert.expiration.notification', current, 'ekcmRecord.common_name');



Thank you.


-Wesley


Wesley,



Your eventQueue should be like this : gs.eventQueue('xteth_ekcm.cert.expiration.notification', ekcmRecord, '', '');


Bingo!   Thanks AKb Zic.


You're welcome