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

Chuck Tomasi
Tera Patron

Hi Wesley,



My recommendation is that you trigger the email with an event. Where you have your red comments, add a line like this:



gs.eventQueue('my.email.trigger', current, '', '');



Next, you will have to register an event and build out the notifications. It's pretty straight forward and documented here.



Email Notifications - ServiceNow Wiki


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.


hello these links are not working. Could you please give me some insights as I also want to send email from script include or if possible by other approaches as well

Wesley Breshear
Tera Expert

Hi Chuck and Pradeep,



I was deferred to another project for awhile, but now back to working on this one.   I followed your steps creating the items in Step 1 and 2 and added the following to the line in red to the code above.   IT WORKED!     Well... sort of.



        if (!ritmRecord.next()) {


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


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


       


The email notification gets sent to the owner when a certificate record is about to expire, but none of values from the script/query are passed to the email.   So the email doesn't tell them information about what or when the certificate is going to expire.   It looks like this.



      Certificate Management


        The following Certificate is expiring.   Please take action to renew or revoke the following Certificate.


       


        Common Name:


        Certificate Type:


        Product Owner Name:


        Valid From:


        Valid To:



        Click here to view: LINK     // link is incorrect too.



Here are the reference fields within the Notification email.


        Common Name: ${common_name}
        Certificate Type: ${certificate_type}

        Product Owner Name: ${owned_by.name}
        Valid From: ${valid_from}
        Valid To: ${valid_to}



Click here to view: ${URI}



Can you assist me on how I would pass the fields (record values) from the query script to the Certificate Expiring Notification email?



Thank you,


-Wesley