script to get specific user and set them into a user reference field

jacobpeterson
Mega Expert

Hello all,

I'm trying to create a UI Policy script that when certain categories are picked it will run the script to get a specific user and sets them into a field.

Here is the script I have so far:

function onCondition() {

var getUser = new GlideRecord('sys_user');

getUser.addQuery('user_name', Jdoe);

getUser.query();

while (getUser.next()) {

  g_form.setValue('approvalRequestedApproval', getUser);

}

}

1 ACCEPTED SOLUTION

Chay2
Mega Guru

Hi Jacob,



Looking at your script, you have hard coded the assignee to "Jdoe"   // getUser.addQuery('user_name', Jdoe);  



1. If you want to assign those the task for that particular person, you can hard code the "sys_id" of "Jdoe" to   approvalRequestedApproval instead of Querying user table using OnChange Client script.


2. If you want to to be it Dynamic based on the requirement you can use http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0    


View solution in original post

8 REPLIES 8

Chuck Tomasi
Tera Patron

Hi Jacob,



I don't see where Jdoe is defined. You need to figure out which user name you are retrieving. This may be a better use case for a client script and a GlideAjax call.



Docs: Client Scripts


Docs: GlideForm


Docs: GlideAjax


Client Script Best Practices - ServiceNow Wiki



     


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Jacob,



Don't use UI Policy if you are having something to do with script. Go with client script. Based on your requirement you can either have onLoad client script or onChange client script.


Do you want to set the value on load of the form or when specific category is selected.




Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Jacob,



I'm not sure which field exactly you want to set from the sys_user table. It looks like you have hard coded the user "JDOE" (currently value hard coding is missing in your script i.e   getUser.addQuery('user_name', Jdoe); should be getUser.addQuery('user_name', 'Jdoe');



Can you please give more info on value you want to return from the sys_user table.


Nate23
Mega Guru

1) UI Policy scripts run client side. GlideRecord is not recommended to use client side. I would use GlideAjax depending on what your condition is to get the user.



2) How do you know what user to assign it to(is it static/dynamic)? Depending on how you answer this tells use what method to use ie: GlideAjax vs. onDisplay Business Rule.



3) The reason it is not working is line 4: Jdoe should be wrapped in quotes like this:


getUser.addQuery('user_name', 'Jdoe');