how can i add watch list users to cc in the email notifications

venkyyy
Kilo Contributor

hello friends, 

i am trying to send the email notifications to the cc ie the user from the watch list.Could you please help me out 

please provide with details as i am new to scripting.

Thanks,

venkat

 

20 REPLIES 20

venkyyy
Kilo Contributor

find_real_file.png

You've removed the default mail script syntax that populates when you create a new mail script, you need to keep that in, your complete mail script should look like below:

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
if(!current.watch_list.nil()){
	var watcherIds = current.watch_list.split(',');
	for (var i=0; i< watcherIds.length; i++) {
		var user = new GlideRecord("sys_user");
		user.addQuery("sys_id", watcherIds[i]);
		user.addQuery("notification",2);
		user.addQuery("email","!=","");
		user.query();
		if (user.next()){
			email.addAddress('cc', user.email, user.getDisplayValue());
		}
		else{
			email.addAddress('cc', watcherIds[i]);
		}
	}
}
	
	
})(current, template, email, email_action, event);

 

rename it:   add_watchlist_to_cc

 

and call it in your message body with:   ${mail_script:add_watchlist_to_cc}

venkyyy
Kilo Contributor

No it didn't fetch me the required result 

Add in some debugging lines and put in the responses here, i've added some examples in the code below, you can find the outputs in script log statements. You need to learn to debug your code, coming back on here and just saying 'it didn't work' isn't very helpful.

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
if(!current.watch_list.nil()){
	var watcherIds = current.watch_list.split(',');
	gs.log('Hi, i made it here and this is the watch list: ' + watcherIds);
	for (var i=0; i< watcherIds.length; i++) {
		var user = new GlideRecord("sys_user");
		user.addQuery("sys_id", watcherIds[i]);
		user.addQuery("notification",2);
		user.addQuery("email","!=","");
		user.query();
		gs.log('there are ' + user.getRowCount() + ' records returned');
		if (user.next()){
			gs.log('good news, i made it here as well');
			email.addAddress('cc', user.email, user.getDisplayValue());
		}
		else{
			email.addAddress('cc', watcherIds[i]);
		}
	}
}
	
	
})(current, template, email, email_action, event);

Hi Please remove the CC: from ${mail_script:new_mail_script_name}, it should work

 

Thanks