Populate field with ID value of user when NAME is selected in field

asd22
Tera Contributor

Hello.

i have 2 fields NAME and ID

asd22_0-1683712993069.png

 

I want the ID field to be filled with the user ID of the selected user in the NAME field. so if i select john, his ID will be auto filled in the ID field.

 

The ID field needs to get infro from here(user_name is the id):

asd22_0-1683713120887.png

 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

 

Write a onChange client script on your variable Name and add code liek this.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var userref = g_form.getReference('Name', userLookup);
	  function userLookup(userref){
	    g_form.setValue('ID', userref.user_name);
	  }
}

Mark the comment as a correct answer and also helpful if this has helped to solve the problem.

View solution in original post

3 REPLIES 3

Pratik Malviya
Tera Guru

Hi @asd22 ,

 

As per the recent upgrade of ServiceNow, you can directly add a reference of the Name (Sys_user) field in the Auto-populate section of the variable "ID"
Open your "ID" variable in the Auto populate section In the Dependent Question add your "name" variable and in Reference add field which you want to auto-populate from user table.  

PratikMalviya_0-1683714364398.png

Please mark it correct if it works for you.

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks,
Pratik Malviya

asifnoor
Kilo Patron

Hi,

 

Write a onChange client script on your variable Name and add code liek this.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var userref = g_form.getReference('Name', userLookup);
	  function userLookup(userref){
	    g_form.setValue('ID', userref.user_name);
	  }
}

Mark the comment as a correct answer and also helpful if this has helped to solve the problem.

Palak Gupta
Tera Guru
Tera Guru

Hello @asd22 ,

You can write an OnChange catalog Client script for this.

Regards,

Palak Gupta