Removing Emojis from emails - without converting to String first

Carl Fransen1
Tera Guru

Hi Team,

We receive a number of emails in our instance with Emoi's - Out of the box ServiceNow doesn't handle these and the emails are cut-off from the place where the emoji is, which means we lose critical information.

I implemented the below Business rule, before/insert with the code below which works great.

(function executeRule(current, previous /*null when async*/) { 
 
// emoticons truncate emails, so remove them prior to insert 
gs.log ("Cleaning up Email to remove Unicode"); 
 
current.body_text = current.body_text.toString().replace(/^[\0\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]/g, ' - '); 
current.body = current.body_text.toString().replace(/^[\0\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]/g, ' - '); 
	
})(current, previous);

But (there's always a but!) as the email is now converted to String the email loses it's HTML and now looks like the below:

find_real_file.png

Without the rule it looks like this - much friendlier for our users:

find_real_file.png

Can anyone shed some light on how I could remove Emoji's without converting to a String first?

Appreciate any assistance!

Thanks Carl.

8 REPLIES 8

Trupti6
Tera Expert

Hi,

Please check for below link,

Tip: Images in Express, and in emails Tip: Images in Express, and in emails

this may help you. 

 

Thanks,

Tripti S.

Hi Tripti,

I'm not sure how that article helps - I'm not looking to manage images, but stop Emoji chars from cutting off the emails.

Thanks

Carl.

Carl Fransen1
Tera Guru

I originally followed this article for the fix.

 

However - recommended fix 1 didn't work so I used fix 2 which converts it to String first.  Has anyone successfully implemented the first example function, as shown below, to work correctly on their instance?

  1. Create a new Business Rule
  2. Table sys_email
  3. When to run: before insert
  4. Enable the Advanced checkbox
  5. Empty the Script field and insert the following code:
function remove4ByteUTF8Unicodecharacters(a) { return a ? a.replace(/^[\0\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "") : "" };

 

Thanks.

Good morning Carl,

I too would like to see a working version of that script as well.  I did note your existing business rule, but as you stated, it effectively strips out the HTML email and makes it plain text, so the formatting goes awry in those cases.

After the Kingston upgrade, I could see that the email body strings are set to string (UTF-8) so when looking at System Logs > Emails, I could see the emojis in the body when received.  There looks like the comments and work notes, being journal fields, can't handle UTF-8 fully hence the cutoff of the emojis when the incident / task etc is being updated with those comments.

It'd either be sensible for SN to fix the settings for those journal fields or provide a working script that can cut it out from the HTML body directly - even possibly have it as an email system option to make it easier for admins.