Not able to Set the value in the Variable via Catalog Client Script

NehaSNOW
Tera Guru

Hi Developers,

 

I have created 2 fields in a Catalog i.e. Employee Name (requested_for_employee_name) and Employee ID (requested_for_employee_id). Employee Name is a Reference field to the User (sys_user) table.

I have written the below Catalog Client Script on Employee Name field to set the Employee ID field with the Employee Number from User Table. 

The issue is Employee ID field is not setting up with the employee number when value in the Employee Name field changes. Please suggest.

 

Catalog Client Script: -

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var EmpSysId = g_form.getValue('requested_for_employee_name');

    var UserSysId = new GlideRecord('sys_user');
    UserSysId.addQuery('sys_id', UserSysId);
    UserSysId.query();
    while (UserSysId.next()) {
        g_form.setValue('requested_for_employee_id', UserSysId.employee_number);
    }
}
 
Thanks,
Neha

 

 

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@NehaSNOW

you can use getReference with callback rather than GlideRecord

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    if (newValue == '') {
        g_form.clearValue('requested_for_employee_id');
    }

    var ref = g_form.getReference('requested_for_employee_name', callBackMethod);
}

function callBackMethod(ref) {
    if (ref.employee_number)
        g_form.setValue('requested_for_employee_id', ref.employee_number);
}

OR

you can do this without scripting with auto populate feature

Auto-populate a variable based on a reference type variable (Utah) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

You would use EmpSysId in the addQuery, not UserSysId.  GlideRecords are not supported or recommended in Client Scripts.  You should instead be in the habit of using getReference

https://www.servicenow.com/docs/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/... 

or GlideAjax to call a Script Include

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

both excellent tools to have in your belt.  In this case, you can also use the variable Auto-populate feature on the definition of the Employee ID to lookup the field from the Employee Name

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc... 

I was not using getReference as it is not good from Performance point of view.

Juhi Poddar
Kilo Patron

Hello @NehaSNOW 

Few things to note:

  • Make sure the employee number field has value. 
  •  Glide record cannot be used in client script as this API supports server side scripting. 
  • Since both the variables are populated from same table, this can be achieved using dot walking and no scripting is required.

Refer the below screenshot to auto-populate the Employee ID variable.

JuhiPoddar_0-1736975940216.png

The dependent question: Employee Name

Dot walk to Employee number field.

JuhiPoddar_1-1736976013002.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Hello @NehaSNOW 

Thank you for marking my response as helpful.

As per the new community guidelines you can mark multiple response as correct answers.

If my response helped you, kindly mark it as an accepted solution.

It helps future readers to locate the solution easily in community.

 

Thank You

Juhi Poddar