- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 12-16-2019 07:36 AM
Hi All,
So we all know that additional comments on any task form come in string form right? Well, whenever my users want to do something a little bit extra, whether that's inserting an image, attaching a document or leveraging one of our range of lovely quick messages rendered in glorious HTML, they open up the email client.
The email client can be enabled on any table by following the procedure outlined here.
Once your users have sent their email they can see the sent mail in the activity log, as long as they have sent/received emails ticked in the filter options (located at the top right of the activity log if you're not aware of it). This is fine in most cases but if you expose records to customers or end users on the service portal, or if your users don't want their activity log clogged up with email records, then you'll need to copy that content into the comments.
Obviously, you won't be able to display whatever wonderful HTML you've configured but you can grab the plain text and stick it in your plain old string field. The only issue is that you'll need to suppress the subsequent notification otherwise you might find yourself accidentally spamming your customers with duplicate messages.
So, the method as detailed in the script below is defined in an after insert/update business rule on the sys_email table. The conditions ensure the rule only runs when there is an instance, the type is 'sent' and the headers contain the 'X-ServiceNow-Source: EmailClient' syntax which is added in when messages are sent from the email client.
In the version below i've opted to remove the HTML tags with a specific function as it removes an extra line that otherwise persists despite desperate application of .trim(). I've commented out an alternative to the removeTags function and the subsequent lines of string manipulation which you can use if you're less fussy about your strings than i am 🙂
This code will move across any attachments sent with your email to the incident and will leave worknotes to that effect to let the world know, it will then copy across the message body, up until the watermark, to the comments on your record.
//condition, to be entered in the 'Condition' field on your business rule:
!current.instance.nil() && current.type == 'sent' && current.headers.indexOf('X-ServiceNow-Source: EmailClient') > -1
(function executeRule(current, previous /*null when async*/) {
appendAttachment();
function appendAttachment() {
var instance = current.instance;
var targetTable = current.target_table;
var count = 0;
var fileName = '';
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',current.sys_id);
gr.query();
if(gr.hasNext()){
while(gr.next()){
fileName = gr.file_name;
gr.table_sys_id = instance;
gr.table_name = targetTable;
gr.update();
updateRecord(targetTable,instance,count,fileName);
count++;
}
}
else{
updateRecord(targetTable,instance,count,fileName);
}
}
function updateRecord(targetTable,instance,count,fileName){
var gr = new GlideRecord(targetTable);
gr.addQuery('sys_id',instance);
gr.query();
if (gr.next()) {
if(count == 0){
//var body = '[code]' + current.body + '[/code]';
var body = removeTags(current.body.toString());
var index = body.indexOf('Ref:MSG');
var msg = body.substring(0, index).trim();
var comment = '*Comment sent from Email Client*\n\n' + msg;
gr.comments.setJournalEntry(comment, current.user_id.getDisplayValue());
}
if(fileName != ''){
gr.work_notes = "Attachment " + fileName + " sent from Email Client by " + current.sys_created_by;
}
gr.update();
}
}
function removeTags(string){
return string.replace(/<br\/>/gm, '\n')
.replace(/(<([^>]+)>)/ig, '')
.replace(/ /gi,' ')
.replace(/&tab;/gi,' ')
.replace(/&/gi,'&')
.replace(/>/gi,'>')
.replace(/</gi,'<')
.replace(/'/gi,"'")
.trim();
}
})(current, previous);
In order to suppress the notification that this will otherwise trigger, you're going to have to add an advanced condition to whatever notification goes out when additional comments changes, in most cases it will be 'Incident commented for ESS'.
The script below will check for the syntax added to the comment in the script above and stop the notification from sending accordingly.
var comment = journalDissection(current.comments.getJournalEntry(1));
if(comment.startsWith('*Comment sent from Email Client*')){
answer = false;
}
else{
answer = true;
}
function journalDissection(journal){
var index = journal.indexOf('\n');
return journal.substring(index,journal.length).trim();
}
So, there you go. Your incidents are now being updated with stuff you're sending in the email client so you can get rid of those pesky sent/received email activities and your service portal users can see the whole conversation. Let the good times roll.
Cheers
Dave
- 2,821 Views