How to return sys_user.user_name from the sc_req_item Requested_for field

Colleen1
Tera Contributor

I am new to ServiceNow. We have added a new field to our sc_task table called u_requested_for_userid.  This field is to capture the user's id value.  In other words, the value you see in the UserID field on the sys_user table but the field name is 'user_name'.  For the most part it is the lastname+firstinitial (one's log id).  I have tried the following code within a new business rule and cannot get the field populated. I even tried dot walking the value and returns a blank.  Any assistance would be greatly appreciated.

For example, 

Requested_for = John Smith

The value I need from the user table is SmithJ

find_real_file.png

(function executeRule(current, previous /*null when async*/) {

populateRequestForIDField();

function populateRequestForIDField() {
//If we have a request item, get the original request
if(!current.request_item.nil()) {
var request_item = new GlideRecord('sc_req_item');
if(request_item.get('sys_id', current.request_item)) {
current.u_requested_for_userid = request_item.request.requested_for.user_name;

}
}
})(current, previous);

 

1 ACCEPTED SOLUTION

Hi there,

I tested with a before insert Business Rule on sc_task with below code, works.

Though, your field is called User ID. So I guess you would like to have the User ID and not the Display Name of the User? Looking at your table image, you made User ID a reference, to sys_user. So this won't work 😞 You have to make this a String field (then Brian's script will work).

Have you also considered just adding the dotwalked field from request to the form lay-out? Or is there a specific reason to have the requested for actually added to the sc_task table?

(function executeRule(current, previous /*null when async*/) {

	current.u_requested_for_userid = current.request.requested_for;

})(current, previous);

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

6 REPLIES 6

Hi there,

I tested with a before insert Business Rule on sc_task with below code, works.

Though, your field is called User ID. So I guess you would like to have the User ID and not the Display Name of the User? Looking at your table image, you made User ID a reference, to sys_user. So this won't work 😞 You have to make this a String field (then Brian's script will work).

Have you also considered just adding the dotwalked field from request to the form lay-out? Or is there a specific reason to have the requested for actually added to the sc_task table?

(function executeRule(current, previous /*null when async*/) {

	current.u_requested_for_userid = current.request.requested_for;

})(current, previous);

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

You are a genius!!! When I changed the type to be a string, it worked!!! Thank you so very much!!!