- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2016 06:10 PM
Hi all,
I was able to import the middle initial from AD and place it in the Middle Name field in ServiceNow but the problem I am having is that when a user doesn't have a middle name, the sys_user.name adds an extra space between the first and last name.
I don't know too much about scripting but this is what I did.
Out of the box the sys_user.name is calculated using the following:
if (current.first_name.nil() && current.last_name.nil() && !current.name.nil()) {
var names = current.name.toString().split(" ");
if (names.length > 1) {
current.first_name = names [0];
names.shift();
current.last_name = names.join(" ");
} else
current.last_name = names [0];
}
if(current.first_name.nil()) {
current.last_name;
} else {
current.first_name + ' ' + current.last_name;
}
I then modified the the calculated value to include an additional "if" statement at the end
if(current.middle_name.nil()) {
current.first_name + ' ' + current.last_name;
} else {
current.first_name + ' ' + current.middle_name + ' ' + current.last_name;
}
The name field now displays the middle initial if the user has one but the problem now is that if the user doesn't have one it creates an extra space between the first and last name. Is there a way to remove the extra space if the user doesn't have a middle name?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019 11:26 AM
I did somewhat find a solution to display the middle initial but I decided not to roll it out because displaying it would have caused confusion for our end users.
When searching for a user using a reference field it would require our end users to know a person's middle initial when searching for them. If they just typed the person's first and last name it wouldn't find the right person until they inputted a middle initial.
But the script to bring in the middle initial is below if you want to try it out.
//checks to see if the user has a middle name. If not display first name, last name and email
if(current.middle_name.nil()) {
current.first_name +" "+ current.last_name +" ("+ current.email +")";
}
else {
current.first_name + ' ' + current.middle_name + ' ' + current.last_name +" ("+ current.email +")";
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2016 07:14 PM
1. Did you verify there are no trailing/leading spaces on the first name or last name fields?
2. Does it add the extra space for ALL users without middle name? If so, does changing current.first_name + ' ' + current.last_name; to current.first_name + current.last_name; fix the issue?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2016 08:13 PM
It seems like you have two options both of which do modify the OOB functionality of sys_user.name dictionary entry. 1) modify the calculated field code like you are attempting or 2) uncheck calculated on sys_user.name.
For #1 you may want to add .trim() on your import to ensure no trailing spaces are coming in. Then start logging until you figure out your logic for adding middle initial when needed.
For #2 Take a look at this thread Problems if sys_user.name field is populated via LDAP import vs. calculated? I did not know that the calculated code is executed every time the record is read. I may actually refactor my LDAP import and turn off field calculation. Better to not have to update code in two locations Transform Map + calculated field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019 07:07 AM
Did you ever find a good solution to this? It seems like it should be simple enough but I am having the same exact issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019 11:26 AM
I did somewhat find a solution to display the middle initial but I decided not to roll it out because displaying it would have caused confusion for our end users.
When searching for a user using a reference field it would require our end users to know a person's middle initial when searching for them. If they just typed the person's first and last name it wouldn't find the right person until they inputted a middle initial.
But the script to bring in the middle initial is below if you want to try it out.
//checks to see if the user has a middle name. If not display first name, last name and email
if(current.middle_name.nil()) {
current.first_name +" "+ current.last_name +" ("+ current.email +")";
}
else {
current.first_name + ' ' + current.middle_name + ' ' + current.last_name +" ("+ current.email +")";
}