Auto Populate Fields

vidhya_mouli
Giga Sage

I have a form which has a reference field - USER (sys_user)

 

This form has other fields like - First name, Last name, Manager (all these are read only fields)

 

When a user chooses from USER, other fields must populate automatically. How can I do this?

1 ACCEPTED SOLUTION

Vallabh Baradk2
Giga Guru

Hello Vidhya,

You can either dot walk the fields as shown below link.

 

https://www.servicenow.com/community/de-germany/how-can-i-populate-the-first-name-and-last-names-of-...

 

or else if you want to do scripting, you can use GlideAjax api as per below

 

var caller=g_form.getValue('caller');

var ga=new GlideAjax('script_include_name');

ga.addParam('sysparm_name', 'getDetails');      //function name 'getDetails'.

ga.addParam('sysparm_value', caller);   //pass caller parameter

ga.getXML(getValue);    //response async call

 

write script include with the function getDetails and apply logic , do gliderecord for user table and retrieve all your return values in one  JSON. 

function getValue(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

}

get that response from script include in JSON , so that you can autopopulate your fields as below,

g_form.setValue('first_name',answer.firstName);

g_form.setValue('last_name',answer.lastName);

 

If you want to know the logic of GlideAjax  you can refer the link below.

 

https://docs.servicenow.com/bundle/tokyo-application-development/page/script/ajax/topic/p_AJAX.html

 

Please mark me correct or helpful if you satisfied with my answer.

 

 

Thank you.

Vallabh

View solution in original post

3 REPLIES 3

Vallabh Baradk2
Giga Guru

Hello Vidhya,

You can either dot walk the fields as shown below link.

 

https://www.servicenow.com/community/de-germany/how-can-i-populate-the-first-name-and-last-names-of-...

 

or else if you want to do scripting, you can use GlideAjax api as per below

 

var caller=g_form.getValue('caller');

var ga=new GlideAjax('script_include_name');

ga.addParam('sysparm_name', 'getDetails');      //function name 'getDetails'.

ga.addParam('sysparm_value', caller);   //pass caller parameter

ga.getXML(getValue);    //response async call

 

write script include with the function getDetails and apply logic , do gliderecord for user table and retrieve all your return values in one  JSON. 

function getValue(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

}

get that response from script include in JSON , so that you can autopopulate your fields as below,

g_form.setValue('first_name',answer.firstName);

g_form.setValue('last_name',answer.lastName);

 

If you want to know the logic of GlideAjax  you can refer the link below.

 

https://docs.servicenow.com/bundle/tokyo-application-development/page/script/ajax/topic/p_AJAX.html

 

Please mark me correct or helpful if you satisfied with my answer.

 

 

Thank you.

Vallabh

Community Alums
Not applicable

Hi @vidhya_mouli ,

 

You don't need to create and use custom fields for this, you can simply dot walk and add fields to the form.

Go to > Form < Additional actions > Configure > Form Layout

Select the reference field and click on expand reference field, than add the field you required in the form.

 

 

If you want to use the custom field, you can do this by creating client script and script include.

I am adding script for your reference.

AnandMahajan_0-1677585944271.png

 

AnandMahajan_1-1677585960830.png

 

If this helps you in solving your problem, mark this as correct & helpful.

 

Regards

Anand