How Do I Auto Populate Form Field based on Selected Value Of A Reference Field

chriscorbett
Giga Contributor

Good Morning All,

I am an experienced programmer but I've been working with ServiceNow for only three weeks. As my title states, I am trying to auto populate a field on my form based on what is selected in a Reference Field in the same form. I found that this same question has been asked before - Auto Populate Field from Reference Field   but I'm asking again as my solution based on the responses there did not seem to work.

I have two tables inside of a custom application I've called Global Data - Employees and Managers. Employees has fields for Employee Name and Employee ID. Managers has a Manager Name field which is a Reference field pointing to the Employee table, and Manager ID. I also display the Employee ID field here from the Employee table.

Based on the replies in the post I mentioned above, I have this script:

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

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

          return;

    }

var empID = g_form.getValue("x_myi_globaldata_employees.manager_name.employee_id");

g_form.setValue("x_myi_globaldata_managers.manager_id", empID);

}

What's happening now is when a name is chosen in the Manager's Name field, the Employee ID field gets populated but the Manager ID does not.

I don't think what I'm trying to do is dependent on an individual sys_id as in the above example so I did not include that. I also have the field names tailored to my particular instance.

I would appreciate any guidance the Community can provide on this issue.

Thanks,

Chris

14 REPLIES 14

I also put in an alert(ManagerID); to see if it would pop up and it did not. So I wonder if there isn't something else happening?


Make sure the reference variable name is chosen in the client script?


Thanks Chris.



When I make the getReference call, should I refer to the table where I have my reference field or the table where the field I'm trying to pull resides?



I changed it to



var ManagerRef = g_form.getReference("x_myi_globaldata_managers.manager_name");


var ManagerID = ManagerRef.employee_id;



And got nothing.


Chris,



Are you ever going to change the value of these fields? Would dot walking to the fields work for you?


I'm looking to change the value of the manager_id field that resides in the managers table. If you look above, I have the line



var ManagerRef = g_form.getReference("x_myi_globaldata_employees.full_name");



Is this not dot walking to the value or is there another way?