- 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
03-11-2016 05:02 AM
Man, this is so frustrating. I put that in and I still got the garbled mess with the org.mozilla.javascript.... I tried you script and other variations in RegEx and they seem to work just fine in there. It's like the script is fine, but servicenow doesn't know how to read and/or translate it. Is there something that I need to put in the rest of my script to make able to read regular expressions? or some property I have to update?
This is my whole script for the inbound action, maybe something in there I'm not seeing is messing with this function?
// Term
createRequest();
event.state="stop_processing";
function createRequest() {
// sep the new hire name
var arrName = email.body.name.split(',');
var lastName = arrName[0];
var firstName = arrName[1].split(' ')[1];
var emp = ((firstName) + ' ' + (lastName));
//get manager
var mgr = '';
var mgrFromEmail = email.body.supervisor;
var mgrRecord = new GlideRecord('sys_user');
if (mgrRecord.get('name', mgrFromEmail)) {
mgr = mgrRecord.sys_id;
}
///(0)(\d+)/
//separate the department
//var arrDept = email.body.department.split('0');
//var number = arrDept[1].split(' ')[0];
//var dept = email.body.department.match(/(0)(\d+)/);
//var dept = (('0') + (number));
var dept = email.body.department.match(/(\d{5})/);
var itemId = gs.getProperty("com.arg.catalog.onboarding"); //grab unique id of New Hire item
var cart = new Cart();
// add in cart
var item = cart.addItem(itemId); //Term sys_id
cart.setVariable(item, 'subject', email.subject); //subject from email
cart.setVariable(item, 'req_dept_num', dept);//dept
cart.setVariable(item, 'req_job_code', email.body.job_code);//job code
cart.setVariable(item, 'req_first_name', firstName); // first name
cart.setVariable(item, 'req_last_name', lastName); //last name
cart.setVariable(item, 'req_state', email.body.state); //state
cart.setVariable(item, 'req_city', email.body.city); //city
cart.setVariable(item, 'req_number', email.body.requisition); //req num
cart.setVariable(item, 'req_start_date', email.body.start_date); //start date
cart.setVariable(item, 'req_hiring_mngr', mgr); //supervisor
cart.setVariable(item, 'req_title', email.body.title); // title
cart.setVariable(item, 'req_status', email.body.type); // type
var rc = cart.placeOrder();
}
I really do appreciate your time in helping me with this!!

- 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
03-14-2016 06:45 AM
That worked PERFECTLY! Thank you so much. I definitely appreciate your time and patience. I have never used regular expressions before. I'm going to have to save both of the links you shared and read up on them!!
Thanks, Brian!