- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 03:52 PM - edited 04-18-2023 03:56 PM
Hi there,
I am working on a new catalog item that generates an incident. I have a requirement to auto-populate some location-related catalog item variables such as "county_name" and "county_code" based on values from the user's (sys_user) location (display value fields from the user's location from cmn_location). The following onChange script is working correctly only in catalog view (activates when user enters something for "username" variable).
The script basically looks at the requested_by on the incident, does lookup on sys_user, then does lookup of user's location on cmn_location, does another lookup on cmn_location to get the values of "name" and "u_school_id" from the parent.
Again it works in catalog view but for some reason this will not work at all in the portal view. I have changed the "UI Type" to "All" but that did not help, it still only works in catalog view (when I click "Try it"). Can anyone advise what I am doing wrong? Please share any code/corrections to get this working in the portal.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//GlideRecord the company table and make sure the vendor has a primary contact with a valid email address
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', g_form.getValue('requested_by'));
user.query();
if (user.next()) {
if (user.location == '') {
g_form.setValue('district_name', 'NODIST');
} else {
g_form.setValue('district_name', user.location);
var grLoc = new GlideRecord('cmn_location');
grLoc.get(user.location);
g_form.setValue('district_number', grLoc.u_school_id);
g_form.setValue('county_name', grLoc.parent);
var grParentLoc = new GlideRecord('cmn_location');
grParentLoc.get(grLoc.parent);
if (grParentLoc == '') {
g_form.setValue('county_name', 'NOCOUNTY');
} else {
g_form.setValue('county_name', grParentLoc.name);
g_form.setValue('county_code', grParentLoc.u_school_id);
}
}
}
}
Thanks for any help!
-Chris
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 05:10 PM
Hi @Chris Dea1,
I believe it is a Record producer as it is creating incidents at the backend.
Please note that GlideRecord queries can show random behaviour on client side and not recommended at all.
It is recommended to use glideAjax to call a script include for GlideRecord query.
Refer the below link for one of the examples-
Please mark this response as correct or helpful.
Regards
Harsimranjeet Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 04:49 PM
Hi @Chris Dea1 ,
Hope you are doing well.
Please make sure UI type on the client script form is selected as "All"
Please note that using glideRecord in client script is not recommended by serviceNow instead use gideAjax
Please mark this response as correct or helpful if it assisted you with your question.
Regards,
Harshal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 05:10 PM
Hi @Chris Dea1,
I believe it is a Record producer as it is creating incidents at the backend.
Please note that GlideRecord queries can show random behaviour on client side and not recommended at all.
It is recommended to use glideAjax to call a script include for GlideRecord query.
Refer the below link for one of the examples-
Please mark this response as correct or helpful.
Regards
Harsimranjeet Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2023 07:07 AM - edited 04-20-2023 07:08 AM
Thanks Harsimranjeet! I ended up using glideAjax on client script to call script include like you mentioned. I had to integrate the script logic I had been trying to use into the script include -- to get user's location fields and fields of the location's parent. The article you provided was helpful and also this one I found useful.