The CreatorCon Call for Content is officially open! Get started here.

Auto-populate fields from selected user

Bhadley
Kilo Expert

Hi all,

 

A bit of a beginner when it comes to javascript so I was hoping someone may have a recommendation for me. I've also found various tidbits of information out in the wiki on field auto-populating on forms but couldnt figure out how to piece the information together to make a workable code.

 

I have a form in the service catalog for employee changes. The first field in the form is a reference field to sys_user so that a user can be chosen that will need the changes. Below that I have reference fields for the old information such as department, manager, etc. What I would like to accomplish is that based on the user chosen in the field it will auto populate the rest of the fields with their current information. In the past we have used similar bits of code such as below on each variable to auto populate fields but gs.getUserID grabs the user filling out the form.

 

javascript:var id = gs.getUserID();

var user = new GlideRecord('sys_user');

user.addQuery("sys_id", id);

user.query();

var rc = "";

if (user.next())

      rc = user.manager;

rc;

 

I am guessing I may need to do a Catalog Client Script using onCellEdit or onLoad? But I cant seem to find the correct setup of script to pull the information based on the Reference Lookup sys_user variable of name. Any help or guidance would be much appreciated, thanks!

1 ACCEPTED SOLUTION

justin_drysdale
Mega Guru

You are on the right track!


Use an onChange catalog client script.   It should work for your needs.



var id = gs.getUserID(); is only going to give you the person that is using the form.   This person may or may not be the same as the person who is in the first field... so it is better to get the value of the first field instead.



"I have a form in the service catalog for employee changes. The first field in the form is a reference field to sys_user so that a user can be chosen that will need the changes"



Try this:



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


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


var user = new GlideRecord('sys_user');


          user.addQuery('sys_id',id);


          user.query();


if ( user.next() ) {


  g_form.setValue('u_manager_field', user.manager);


  g_form.setValue('u_last_name', user.last_name);


  g_form.setValue('u_whatever', user.field_on_sys_user);


}


}



For the onChange script variable, choose the user reference field ( the first field on the form ).


View solution in original post

21 REPLIES 21

Hi Bhadley,



Just an addition to the above solution given by Subramanya, the getReference() is actually a synchronous server call which halts the client end until the response comes from the server. Where as if you use the same with a call back method (GlideForm (g form) - ServiceNow Wiki), the other client end scripts won't be halted due to this call since it's asynchronous which improves the client side performance a lil bit.



Ex:


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


userObject = g_form.getReference('user_field',setUserInfo);


}



function setUserInfo(userObject){


g_form.setValue('u_manager_field', userObject.manager);


g_form.setValue('u_last_name', userObject.last_name);


g_form.setValue('u_whatever', userObject.field_on_sys_user);  


}



Please mark answer as correct/helpful, if it was really helpful.



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


http://www.solutioningnow.com/


I love Service-Now just for the fact that there are always multiple ways to accomplish something. Thank you for all your help, the solutions work like a charm!


Hello,



I am using this code below, but I get what looks like a sys_id for output on "Manager", "Company", and "Default Group".



Everything else works fine except no User ID, no Department and middle initial is "undefined".



Any ideas how I can correct the issue with Sys_ID instead of text?



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


  if(newValue == null || newValue == '') {


  return;


  }



  else {


  var caller = g_form.getReference('requested_for', setCallerInfo);


  }


}




function setCallerInfo(requested_for) {


  g_form.setValue('first_name', requested_for.first_name);


  g_form.setValue('middle_initial', requested_for.middle_name);


  g_form.setValue('last_name', requested_for.last_name);


  g_form.setValue('email', requested_for.email);


  g_form.setValue('company', requested_for.company);


  g_form.setValue('department', requested_for.department);


  g_form.setValue('manager', requested_for.manager);


  g_form.setValue('title', requested_for.title);


  g_form.setValue('phone', requested_for.phone);


  g_form.setValue('mobile_phone', requested_for.mobile_phone);


  g_form.setValue('u_default_group', requested_for.u_default_group);


  g_form.setValue('userid', requested_for.user_name);


}


to display the email address of the reference field user would it be the below line?



g_form.setValue('email', userObject.email;


Make sure you set the variable field type to the same type as the field you are referencing. So if you are trying to populate the info for department, you need to make a reference field pointing to the Department table.