2017年8月2日 星期三

[ServiceNow] Force send an email

Method 1 By Server Side script


When creating business rule / scheduled job using this script, make sure the current scope is Global.

var emailNotification = new GlideRecord('sys_email');
emailNotification.initialize();
emailNotification.type = 'send-ready';
emailNotification.notification_type = 'SMTP';
emailNotification.subject = 'Email subject';
emailNotification.body = 'Email body';
emailNotification.recipients = email-address1 + ',' + email-address2;
var sys_id = emailNotification.insert();

Method 2 - Business Rule Before


It seems only "Before" business rule can read the variables on the request form.

Assign the email in the form to a dummy user then send email notification to that user.

(function executeRule(current, previous /*null when async*/) {

var tmpUser = new GlideRecord('sys_user');
tmpUser.get('0a826bf03710200044e0bfc8bcbe5d7a');
if( tmpUser )
{
gs.info('br_catalog_item_before email '+current.variables.email);
gs.info('br_catalog_item_before remark '+current.variables.remark);
// adela.cervantsz@example.com
tmpUser.email = current.variables.email;
tmpUser.update();

gs.eventQueue('x_122591_marketing.yhy_send_email', current, tmpUser, gs.getUserName());
}
else
{
gs.info('br_catalog_item_before nf');
}

})(current, previous);

沒有留言:

張貼留言