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

Hello,



I don't really have a preference between using a UI Policy or a Client Script but the UI Policy seemed easier because I could define the conditions simply.



I have a select box field with different categories in the drop down. When I select certain categories I want the user reference field to populate with 1 specific user. Otherwise the user field is open to select a user.



I changed the JDoe to 'JDoe' but the field returns blank. I've also never used a GlideAjax, would it be as simple as just replacing the GlideRecord?


Hello Jacob,



Thanks for the update. If the user per condition is always static then you need not do GlideRecord or GlideAjax calls and instead can be set via g_form.setValue


i.e g_form.setValue('approvalRequestedApproval', 'sys_id of the user'); //Also make sure field column name is correct i.e approvalRequestedApproval. If this is a custom field it should have prefix(u_) i.e u_approvalRequestedApproval


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    


johnram
ServiceNow Employee
ServiceNow Employee

The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: AJAX
 


Visit http://docs.servicenow.com for the latest product documentation