How to reference a user id using the user_name value?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2013 03:35 AM
I want to use a user_name text box variable in a workflow run script to update a m2m table which has a relationship with our sys_user table.
My problem is the user_name variable is a text box and not a reference field, ServiceNow will not fill in the m2m table user reference with that value.
Here's the code - i'm grabbing some values from an array and then trying to update the m2m table.
The applications go in fine because they are referenced in the array
//make the array and split the values
var str = current.variable_pool.sap_accounts.toString();
var array = str.split(",");
for (var i=0; i
//insert the array values into the m2m table
var application = new GlideRecord('u_m2m_users_applications');
application.initialize();
application.u_application = array;
application.u_user = current.variable_pool.user_name; <<-- problem is here
application.insert();
}
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2013 06:45 AM
The best solution is to change your variable to a reference variable. Your script would work as-is that way. If it has to be a regular text box, then you'll need to query for the user record before trying to populate the 'u_user' field.
//query for the user record from 'user_name'
var usr = new GlideRecord('sys_user');
usr.addQuery('user_name', current.variable_pool.user_name);
usr.query();
if(usr.next()){
//make the array and split the values
var str = current.variable_pool.sap_accounts.toString();
var array = str.split(",");
for (var i=0; i<array.length; i++){
//insert the array values into the m2m table
var application = new GlideRecord('u_m2m_users_applications');
application.initialize();
application.u_application = array<i>;
application.u_user = usr.sys_id.toString();
application.insert();
}
}
else{
gs.addInfoMessage("User name " + current.variable_pool.user_name + ' not found.');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2013 07:27 AM
Hi Mark
It's working as expected.
I toyed with the query idea but couldn't figure out how to grab the SYSID or convert it to a value SNC would take in a reference field.
Live and Learn
Thanks
Neil 🙂