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

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()


Thanks Brad!   I tried the default value first and that did the trick .  


Great. Default is definitely the better way to go as your doing everything server side as the form loads.


Update on my last post - I have the code below working on a 'multi line text' variable to grab the users manager:

 

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

 

However when I use the same default value on my reference field it isn't working.  I'm aware a reference field looks at the sys_id, but not sure how to change the value into the sys_id so it display correctly?

Essentially I want to default the value, but if their manager had changed recently than the users need the ability to select their new manager.  This is why it needs to be a reference field as I'll then also use this value to input into the workflow approvals to ensure the right person is sent the approval.

 

Thanks

Carl.