Reading the DISPLAY NAME from an incoming email

Jacob Saaby Ni1
Giga Expert

Hi everyone.

Ran into a perplexing thing today, which I would like your two cents on:

When receiving an email, it's pretty easy to get certain information from that email: Senders email address, subject, body etc.

But - what if I just want to get the display name of the email sender?

I see that it's in the From: in the header. So it says:

From: Jacob Saaby Nielsen <jacob@blabla.com>

How do I get the "Jacob Saaby Nielsen" part from the email headers?

Can't seem to find anything on that.

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

I've searched recently though didn't find anything for this. I was expecting something like "email.user_id".

Ended up with a GlideRecord query on sys_user unfortunately.

See below. Note: this is only part of the whole code!

(function() {
	
	var emailParsed = email.body.caller;
	emailParsed = emailParsed.split(' ');
	
	var grUser = new GlideRecord('sys_user');
	grUser.addQuery('email', emailParsed[0]); // Though, what if this email is not unique?!
	grUser._query();

	if(grUser._next()) {
		var requested_by = grUser.getValue('caller');

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

8 REPLIES 8

Mark Roethof
Tera Patron
Tera Patron

Hi there,

I've searched recently though didn't find anything for this. I was expecting something like "email.user_id".

Ended up with a GlideRecord query on sys_user unfortunately.

See below. Note: this is only part of the whole code!

(function() {
	
	var emailParsed = email.body.caller;
	emailParsed = emailParsed.split(' ');
	
	var grUser = new GlideRecord('sys_user');
	grUser.addQuery('email', emailParsed[0]); // Though, what if this email is not unique?!
	grUser._query();

	if(grUser._next()) {
		var requested_by = grUser.getValue('caller');

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark.

Haven't had time to test it yet. Easter Holiday here in Denmark, so it won't be sometime until next week I'll get around to it.

But I very much appreciate your reply, and will get back to you next week 🙂

Hi Mark and ggg.

Thanks for your replies.

So, ggg - to answer: My requirement is, that in case the sender is unknown to me, I would like to create that sender, as a CSM contact.

For that, I need a name. The only way I can see where I can extract that, is from the email headers. Specifically from:

From:Jacob Saaby Nielsen <name@emaildomain.tld>

So. I believe what Mark answered is part of the solution. The splitting of the string, that follows what ServiceNow will interpret as a variable:value couple. As I understand how ServiceNow interprets variable pairs in an email is that it's variable:value.

So "from" becomes the variable name. And "Jacob Saaby Nielsen <name@emaildomain.tld>" becomes the value.

Using Marks' method, I can definitely see how I can split the value of from, into a string array, and access the various parts.

So from[0] will become "Jacob", from [1] will become "Saaby" and from [2] will become "Nielsen".

Which means that I should be able to do:

  • var firstName = from[0];
  • var surName = from[1] + " " + from[2];

Questions:

So, how do I access the email header part of the email, to begin with?

If I know that, I believe I can put together a script, which tests for a known user vs unknown user, and if unknown user, create a contact.

Even though the value part of the from differs wildly. But that part's out of my hands.

@Jacob Saaby Ni1 

hi Jacob,

It has been a while, but I faced today a similar type of situation. Some scripting is required to get the display name from the "From" person when (s)he is unknown.

Below a script that did the trick and did some cleansing to. The script can be pasted in the Actions tab in an Inbound Action. I have tested it with several type of accounts, like Gmail, company accounts etc.

The "From" will be stored in this example in a separate field, though you could also create a contact or do anything else with it.

 

 

 

//If Consumer is unknown, than get the display Send From out of the mail header
    if (current.consumer.nil()) {
        var start = email.headers.indexOf('\nFrom') + 5;
        if (start > 0) {
            var end = email.headers.toString().substring(email.headers.toString().indexOf('\nFrom')).indexOf('<');
            var name = email.headers.toString().substring(start, start + end - 5);
            if (name.charAt(0) == ':') name = name.slice(1);
            name = name.replaceAll('"', '');
            current.u_opened_for_name = name;
        }
    } else {
		//Set name from Consumer when known
        current.u_opened_for_name = current.consumer.user.name;
    }

 

 

 

Regards, Peter