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 05:40 AM
Try using getReference
if (isLoading || newValue == '') {
return;
}
var empID = g_form.getReference("x_myi_globaldata_employees.manager_name");
var ID =empID.employee_id;
g_form.setValue("x_myi_globaldata_managers.manager_id", ID);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 06:18 AM
Thanks Ravi, for your reply.
My script now looks like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ManagerRef = g_form.getReference("x_myi_globaldata_employees");
var ManagerID = ManagerRef.employee_id;
g_form.setValue("x_myi_globaldata_managers.manager_id", ManagerID);
}
and it is still not working.
On the var ManagerRef line, I tried it with g_form.getReference("x_myi_globaldata_employees.full_name") and g_form.getReference("x_myi_globaldata_employees")
And it did not work either way.
Any other suggestions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 06:23 AM
Make sure your variable names are correct.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ManagerRef = g_form.getReference("x_myi_globaldata_employees.full_name");
var ManagerID = ManagerRef.employee_id;
g_form.setValue("x_myi_globaldata_managers.manager_id", ManagerID);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-01-2016 06:38 AM
Done. Still no go.
Does it matter at what level the client script is applied? Right now I have it set on the entry form level where I enter a new Manager.