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

Brian Lancaster
Tera Sage

Just to clarify the u_requested_for_userid is a filed on the sc_task table and not a catalog item variable correct?

Colleen1
Tera Contributor

It is a new field I added to the sc_task table (excerpt below).

 

find_real_file.png

Then you business rule should be as simple as below.  This is a before insert running on the sc_task table. If you do it as an after insert you will need to add current.update();

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

	// Add your code here
	current.u_requested_for_userid = current.request.requested_for.user_name;

})(current, previous);

Thank you for the response.  I applied the code and it is still returning a blank value??  Any other ideas or suggestions?