How can i get department value for a user in default value.

Rithu
Kilo Contributor

Hi,

 

I have a field called Assigned to (assigned_to) which is referring to the User (sys_user) table on an incident form. I have a new field called department on the incident form. Based on the assigned to value, it should retrieve department and populate in the field. Can i do it through default value code on the variable. If so, what should be the expression.

 

Thanks.

1 ACCEPTED SOLUTION

Chaitanya Redd5
Tera Guru

Hi Rithu,

You can bring the department field itself from the user table on the form, no code is needed. Please follow the below steps and you can achieve this:

1. Open the form layout of the Incident form.

2. Select the assigned to and click on the highlighted icon in the below snipped.

find_real_file.png

 

3. Select the department and bring it in the selected box and place is where you department field to be.

find_real_file.png

4. Save, Now the Assigned to's department field from the user table will come up on the incident form. No fields or script needs to be created.

Note- For security purpose you need to make the department field read only or else modifying the field could modify the data on user table.

 

Kindly mark my answer as Correct and helpful based on the Impact.

Regards,

Chaitanya

View solution in original post

14 REPLIES 14

Sumanth16
Kilo Patron

Hi Rithu ,

 

You can put the default value in that field:

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

 

(or)

 

 Please write onchange client script on incident table , when "assigned to" field changes

Try below code:

function onChange(control, oldValue, newValue, isLoading) {

 
var id = g_form.getValue('assigned_to');//replace with the name of your department reference field.

 
var user = new GlideRecord('sys_user');

 
          user.addQuery('sys_id',id);

 
          user.query();

 
if ( user.next() ) {

 
  g_form.setValue('u_department', user.department);//replace with your department field name

 
 

 
}

 
}

 

Please try it and let me know.

 

Please mark it as helpful (or) correct if it helps.

 

Thanks,
Sumanth

asifnoor
Kilo Patron

In default, you cannot do it, becuase default value works at the server end where as you want the department to be set based on the value of assigned to.

But you can do this with the reference specification. Make the department field refer to department table and under reference specifications, mention the reference qualifier as advanced and mention the below code.

'javascript:sys_id='+gs.getUser().getUserByID(current.assigned_to).getDepartmentID()

Under attributes, mention

ref_qual_elements=assigned_to

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Rithu
Kilo Contributor

This is not working. Department value is not populating

Try below:

gs.getUser().getDepartmentID();

For more reference:

https://www.servicenowguru.com/scripting/user-object-cheat-sheet/


Please mark my response as correct and helpful if it helped solved your question.
-Thanks