How Do I Auto Populate Form Field based on Selected Value Of A Reference Field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 05:31 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 07:44 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 07:57 AM
A Manager is an employee. Their name and employee id will be in the Employees table.
When an entry is created on the form for the Managers table, I have Manager ID and Manager Name with Manager Name a Reference field tied to Employees. When you enter a name that auto populates in the Reference field, I'm trying to set it up so that the Manager ID is automatically populated with the Employee ID related to the name I choose in the Reference Field.
I can do this easily outside of SN using PHP or any other language. Being very new to SN, I'm trying to figure out the proper SN way of getting that done. I hope this clarifies what I'm trying to accomplish.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 08:09 AM
Try removing the x_myi_globaldata_employees (this is your table?) I don't think you need that there.
g_form.getReference("full_name");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 08:14 AM
Done. No change.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 10:16 AM
I have deleted and recreated the script with the following:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var ManagerRef = g_form.getReference("manager_name");
var ManagerID = ManagerRef.employee_id;
alert('test');
alert(ManagerID);
//Type appropriate comment here, and begin script below
}
And I am now getting the following error onChange:
onChange script error: TypeError: ManagerRef is undefined function (){var o=i(m,arguments);return l.apply(n,o)}
Any guidance would be appreciated.