Auto populate field from user table to catalog item form based on user

Ankita Gupte
Kilo Sage

Hello Experts,

 

I have a requirement to auto populate values from user table to catalog item form based on selected user name.

 

I have written below onchange client script so whenever primary account name is selected it auto populates value from user table.

AnkitaGupte_0-1676983498363.png

But employee type is not populating. Employee type field is choice field on user table

AnkitaGupte_1-1676983581127.png

below is the onchange client script which fails to populate the employee type.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var caller = g_form.getReference('var_primary_acc_name', doAlert); // doAlert is our callback function
}

function doAlert(caller) { //reference is passed into callback as first arguments

g_form.setValue('var_name', caller.name);
g_form.setValue('var_employee_type', caller.u_employment_type.label);
g_form.setValue('var_job_title', caller.title);
g_form.setValue('var_department', caller.department);
g_form.setValue('var_reporting_manager', caller.manager);
g_form.setValue('var_company_name', caller.company);
g_form.setValue('var_parent_company', caller.u_user_opco);
g_form.setValue('var_office_location', caller.location);
g_form.setValue('var_mobile_number', caller.mobile_phone);
g_form.setValue('var_logon_id', caller.user_name);
}

 

Am I missing something here.

 

 

 

 

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage
Tera Sage

@Ankita Gupte ,

 

I have checked in the PDI and caller.u_employment_type.label is returning "undefined", so it is not working,
Best way to achieve this is:

1) create a json object as 
var json = {"3":"captive","2":"contractor","1":"FTE"};
g_form.setValue('var_employee_type', json[caller.u_employment_type+""]); // caller.u_employment_type will return backend value as 3,2,1 accordingly.

The second method is to use glideRecord on the sys choice table, but I don't think that makes sense because you have to make a glideAjax call to get the values, and the options for your field are limited.

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

View solution in original post

4 REPLIES 4

Pooja Mallikarj
Kilo Sage

Hi,

Try by using Choice field vallue like below.

g_form.setValue('var_employee_type', caller.u_employment_type.value);

 

Thanks,

Pooja M

Prince Arora
Tera Sage
Tera Sage

@Ankita Gupte ,

 

I have checked in the PDI and caller.u_employment_type.label is returning "undefined", so it is not working,
Best way to achieve this is:

1) create a json object as 
var json = {"3":"captive","2":"contractor","1":"FTE"};
g_form.setValue('var_employee_type', json[caller.u_employment_type+""]); // caller.u_employment_type will return backend value as 3,2,1 accordingly.

The second method is to use glideRecord on the sys choice table, but I don't think that makes sense because you have to make a glideAjax call to get the values, and the options for your field are limited.

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Thank You Prince.

 

Its working perfect.

Jyoti Jadhav9
Tera Guru

Hi @Ankita Gupte ,

 

The getReference() method is not considered best practice, even with a callback function, as it retrieves the complete record while usually you only need one field or two.

The recommended best practice options are g_scratchpad and GlideAjax.

From docs: "GlideRecord and g_form.getReference() are also available for retrieving server information. However, these methods are no longer recommended due to their performance impact. Both methods retrieve all fields in the requested GlideRecord when most cases only require one field."

Use GlideAjax to send value to server and then return display name from server.

 

You can refer below thread to achieve the functionality:

https://www.servicenow.com/community/developer-articles/get-user-details-based-on-the-logged-in-user...

 

Please mark this as the correct answer and helpful if it is resolved, or mark this helpful if this helps you to reach the solution.

 

Thanks & Regards

Jyoti Jadhav