- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2016 11:46 AM
Hi everybody!
Basic background on what I have going on:
I am working on an inbound email script where the email comes in with a name like 'Jones, Tommy L (Tom)'.
I need to take this, split it rearrange it, put it back together, and then have it fill in a reference variable. I have it about 90% complete. The snag I am running into is making sure that it fills in this reference with 'Tommy Jones' and leaves off the middle initial and the nickname. I eventually worked my way up to what I want - 'Tommy Jones' - but i accomplished it in a way that I know that will not work for all names coming in:
// sep the term name
var sep = email.body.name.lastIndexOf(',');
var space = email.body.name.lastIndexOf(' ');
var firstname = email.body.name.substring(sep + 2, space - 2);
var lastname = email.body.name.substring(0,sep);
var emp = ((firstname) + ' ' + (lastname));
// Get the Term ref
var term = '';
var termFromEmail = emp;
var termRecord = new GlideRecord('sys_user');
if (termRecord.get('name', termFromEmail)) {
term = termRecord.sys_id;
}
cart.setVariable(item, 'emp_name', term); // term emp
Sometime these names might come in as 'Jones, Tommy Lee' or 'Jones, Tommy (Tom). Instead of the first name reading from the space back (which it doesn't seem to be doing properly anyway), i want it to read the name until it gets to a space.
Let me know if you guys have any idea or if it needs to be explained better.
Thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2016 06:29 PM
Hi Alex,
Sorry, even with just the one match, it's returning an array. Just reference the first element in the returned array (zero-based) instead, like this:
var dept = email.body.department.match(/(\d{5})/)[0];
I think that should do it. I used the following to test on jsfiddle.net:
var str = 'Whse: 04592 Flooring'
var dept = str.match(/(\d{5})/);
alert("Dept: " + dept[0]);
alert("Dept is type: " + typeof dept);
See if that works for you.
Thanks,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2016 06:45 AM
Okay so i changed it to:
var arrName = email.body.name.split(',');
var lastName = arrName[0];
var firstName = arrName[1].split(' ')[1];
This seemed to work splendidly!
Thank you for your help, Brian!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2016 12:13 PM
Hey Brian, I was hoping to solicit your help again. I found a flaw with this particular script. this is what I have in there now:
var arrDept = email.body.department.split('0');
var number = arrDept[1].split(' ')[0];
var dept = (('0') + (number));
The issue is, if the department through the email comes in as "Whse: 04901 Flooring", it will pull and put "049" in the variable. So, it looks like after it find the first 0 any ones after it are treated like a separator. I really only want it to be able to find the first one so that "04901" prints in the variable box.
Any thoughts on how to modify this?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2016 01:03 PM
Hi Alex,
You don't want to use "split" on a value that you wish to keep... so if "0" is a part of your department value, don't split using it. Use what comes before it instead:
var arrDept = email.body.department.split(' ');
var detp = arrDept[1];
You could use split on ":" instead, but then you would need to trim the spaces from your result.
var arrDept = email.body.department.split(':');
var dept = arrDept[1].trim();
If your department is always a numeric, and always referenced like this, you might want to use "match" with regex instead of "split". Here's some match and regex refs:
Then you could do something like:
var dept = email.body.department.match(/(Whse: )(\d+)/i)[1];
Or, if it's always a 5-digit number:
var dept = email.body.department.match(/(Whse: )(\d{5})/i)[1];
Try one/any of these and see how it works for you. BTW, if you haven't already, learn RegEx... it's a powerful tool you can use. I'm no master at it, but practice-practice-practice.
Thanks,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2016 01:34 PM
The problem with these is this email comes directly from HR. Which means nothing is consistent. I've seen it come through as ""Whse: 04901 Flooring", "Whse 04901 Flooring", "Whse Flooring 04901", etc. The only thing I can rely on is that somewhere in that string there will be a 5 digit department number starting with '0'. It may or may not be after something like a colon, may or may not be the 2nd word in the string. So I really need something that read the string, finds the '0' and gives me that number and the four after it.
I have never used RegEx and I don't know anyone else who does. So, I have a small question on it. I did some testing and when I added some example department numbers in the text and then changed the top to "/([0])\w+/g", it seemed to comb through and highlight properly:
So I tried it in my inbound script like this:
var dept = email.body.department.match(/([0])\w+/);
And then went into the request item and now logged in the department variable is this:
org.mozilla.javascript.NativeArray@1e1c7be
Should I be putting the 'g' at the end even if I want it to search the string for just the first one? or should I not have included the '/'s? I feel like this is definitely helpful and in the right direction. I really appreciate the references.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2016 02:36 PM
Hi Alex,
- In regex, "\w" will match any alphanumeric, "\d" will match a numeric digit only [0-9]... so I would use "\d".\
- If you only want the first match, don't use the "g" switch.
- Yes, you need the "/"s... that's telling it you are using a regex statement in between them.
If it is always going to be a 5-digit numeric somewhere in there, but the rest is unpredictable, I would use:
var dept = email.body.department.match(/(\d{5})/);
You don't need the "i" switch anymore, since numbers have no case.
-Brian