Formatting journal entries on mail scripts.

Harry Campbell2
Mega Guru

Hello All,

I am looking for a way to nicely format an email notification. In particular when a note is added to an incident. At the moment we are just getting the journal entry and printing it on the notification:

find_real_file.png

I would like to be able to separate out the user who added the notes, the date/time and the note itself so these can be formatted differently.

Can anyone guide me on the best way to achieve this?

Many Thanks

Harry

 

 

1 ACCEPTED SOLUTION

Dubz
Mega Sage

Hi Harry,

 

I asked this same question a while back, Chuck got back with a solution of using getJournalEntry() and parsing the result with a regex and then doing what you want with the output. I couldn't get that solution working but perhaps you will have more luck with it:

 

https://community.servicenow.com/community?id=community_question&sys_id=bc720feddb98dbc01dcaf3231f96...

 

The solution we did get working was done by our implementation partners, it's apparently not best practice as you should use getJournalEntry() but it works! The getUserTime() function is to present the time in the local users time zone, not sure if that's something you'll need but you should be able to modify it to suit your requirements.

 

(function runMailScript(current, template, email, email_action, event) {
	template.print('<p><font size="3" color="#030000" face="arial">');
	template.print(gs.getMessage('Your Incident has been updated with the following comments') + ':');
	template.print('</font></p>');
	
	var custom_comment = new GlideRecord('sys_journal_field');
	custom_comment.addEncodedQuery('element=comments^element_id=' + current.sys_id);
	custom_comment.orderByDesc('sys_created_on');
	custom_comment.setLimit(3);
	custom_comment.query();
	while (custom_comment.next()){
		
		var userGR = new GlideRecord('sys_user');
		userGR.addQuery('user_name', custom_comment.sys_created_by);
		userGR.query();
		if(userGR.next()){
			var getTZ_time = getUserTime(custom_comment.sys_created_on, userGR.time_zone);
			template.print('<div><font size="3" color="#030000" face="arial"><span></span></font><hr></div>');
			template.print('<p><strong><font size="3" color="#030000" face="arial">' + getTZ_time + ' ' + userGR.time_zone + ' - ' + userGR.name +  '</font></strong></p>');
			template.print('<p><font size="3" color="#030000" face="arial"><pre style="background-color:transparent; border: none;">' + custom_comment.value + '</pre></font></p>');
		}
		
		
	}
	function getUserTime(date,usertimezone){
		var timezone = Packages.java.util.TimeZone.getTimeZone(usertimezone);
		var gdt = new GlideDateTime(date);
		gdt.setTZ(timezone);
		var set1 = gdt.getTZOffset();
		gdt.setNumericValue(gdt.getNumericValue() + set1);
		
		return gdt;
	}
		
	})(current, template, email, email_action, event);

View solution in original post

4 REPLIES 4

Dubz
Mega Sage

Hi Harry,

 

I asked this same question a while back, Chuck got back with a solution of using getJournalEntry() and parsing the result with a regex and then doing what you want with the output. I couldn't get that solution working but perhaps you will have more luck with it:

 

https://community.servicenow.com/community?id=community_question&sys_id=bc720feddb98dbc01dcaf3231f96...

 

The solution we did get working was done by our implementation partners, it's apparently not best practice as you should use getJournalEntry() but it works! The getUserTime() function is to present the time in the local users time zone, not sure if that's something you'll need but you should be able to modify it to suit your requirements.

 

(function runMailScript(current, template, email, email_action, event) {
	template.print('<p><font size="3" color="#030000" face="arial">');
	template.print(gs.getMessage('Your Incident has been updated with the following comments') + ':');
	template.print('</font></p>');
	
	var custom_comment = new GlideRecord('sys_journal_field');
	custom_comment.addEncodedQuery('element=comments^element_id=' + current.sys_id);
	custom_comment.orderByDesc('sys_created_on');
	custom_comment.setLimit(3);
	custom_comment.query();
	while (custom_comment.next()){
		
		var userGR = new GlideRecord('sys_user');
		userGR.addQuery('user_name', custom_comment.sys_created_by);
		userGR.query();
		if(userGR.next()){
			var getTZ_time = getUserTime(custom_comment.sys_created_on, userGR.time_zone);
			template.print('<div><font size="3" color="#030000" face="arial"><span></span></font><hr></div>');
			template.print('<p><strong><font size="3" color="#030000" face="arial">' + getTZ_time + ' ' + userGR.time_zone + ' - ' + userGR.name +  '</font></strong></p>');
			template.print('<p><font size="3" color="#030000" face="arial"><pre style="background-color:transparent; border: none;">' + custom_comment.value + '</pre></font></p>');
		}
		
		
	}
	function getUserTime(date,usertimezone){
		var timezone = Packages.java.util.TimeZone.getTimeZone(usertimezone);
		var gdt = new GlideDateTime(date);
		gdt.setTZ(timezone);
		var set1 = gdt.getTZOffset();
		gdt.setNumericValue(gdt.getNumericValue() + set1);
		
		return gdt;
	}
		
	})(current, template, email, email_action, event);

Thanks David, your solution worked perfectly. I love that I can style the individual elements in the mail script.

 

find_real_file.png

David Dubuis - Once again found a solution for me... This time I didn't even need to ask.

Thanks again

Thomas Michels1
Kilo Contributor

This is all great, but if you have more than one line in your comment, it doesn't get printed in the right font and size.

 

BR - Thomas