How do you set the current user on a record producer

ngriffiths
Mega Expert

Hi all,

I am trying to set a reference variable (var = u_requested_for ref table = sys_user) on a record producer to the current user that opened it up.   I believe this will need to be achieved through a Catalog Client Scripts, but for whatever reason it doesn't appear to be working.   I am not a programmer, so my code may be/most likely is wrong.   Any help or solutions would be greatly appreciated!

Catalog Client Scripts -

Name: Set request for variable

Applies to: A Catalog Item

Active: True

Type:   onLoad

Applies on Cat Item view: True

Applies RITM: False

Applies on Cat Tasks: False

Script:

function onLoad() {

      var userID = g_user.userID;

      userID = g_form.setValue('u_requested_for');

}

I also tried g_form.setValue('u_requested_for',gs.getUserID()); but an error came up saying gs shouldn't be used in client scripts.

Thanks!

Niccole

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

For the 3rd line of your script try


g_form.setValue('u_requested_for', userID);



Also, you could do this through the default value of the variable rather than a client script. Try this in the default value: javascript: gs.getUserID()


View solution in original post

8 REPLIES 8

If you want to use the logged in user's manager as the default value I would just do:

javascript: gs.getUser().getManagerID();

Thanks for that Brad - I wasn't sure why I didn't try that one earlier... it works a treat.

 

BTW - I did get my other one working as well eventually late yesterday NZ time - what I got was below, but it's not as nice as your answer!

javascript: var user = new GlideRecord('sys_user'); user.get(gs.getUserID()); user.manager.sys_id;

 

Thanks for your help.

Carl.

Hi Brad,

The javascript bit was very helpful as I was doing this via a Client Script - Thanks!

I have an additional question - I have another variable on a record producer which requires me to grab the users manager - how would I be able to grab this value as a default using the above? 

 

Thanks

Carl.

Henrik Jutterst
Tera Guru

I would use this solution: How to auto populate with current user