How Can I autopopulate 'Location' Reference field with same value as shown in Space Reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2023 12:18 AM
How Can I auto-populate 'Location' Reference field with same value as shown in Space Reference field
Hi Guys,
I have built a few Reference fields, Building (u_building), Floor (u_floor) and Space (u_space) field on my Incident Form (see screenshot attached). These are all Reference fields.
These are all driven/referenced directly from the Location (cmn_location) table.
I also have the standard 'Location' field, which also references the Location table.
How can I write either a script or business rule, so that when the Space (u_space) field gets populated with a location value, at the same time the Location (location) field will then also get updated with the same location value, that is referenced within the 'Space' (u_space) field ?
I assume this is either a Business Rule or perhaps onChange Client Script?
Please see my screenshot attached, showing the relevant fields.
I would appreciate any help/guidance with achieving this.
Many Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2023 03:08 AM
Hey Daniel,
Actually very interesting that you mention this, as the client asked me if it is at all possible to walk up the location hierarchy, so that if Space is selected, then Floor, then Building would auto-populate.
I did not think this was possible but if it is, I would love to be able to configure that.
What would be the script to make this happen, is it straight forward to configure?
Many thanks once again.
Kind Regards, Warwick

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2023 06:08 AM - edited 06-12-2023 11:18 PM
Hi @WazzaJC ,
This location setup as example.
Script Include - Client Callable
var LocationHelperAjax = Class.create();
LocationHelperAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLocationHierarchie: function() {
var space = this.getParameter('sysparm_space');
var result = {};
this.getRecursiveHierarchie(space, result);
return JSON.stringify(result);
},
/*
* recursive function crawls all location from hierarchie and store it in resultObject with key
* cmn_location_type and as value a object with sys_id and display_name of the location.
*/
getRecursiveHierarchie: function(location_id, resultObject) {
if (JSUtil.notNil(location_id)) {
var grLocation = new GlideRecord('cmn_location');
if (grLocation.get(location_id)) {
var locObj = {
sys_id: grLocation.getUniqueValue(),
display_value: grLocation.getValue('name')
};
resultObject[grLocation.getValue('cmn_location_type')] = locObj;
if (JSUtil.notNil(grLocation.parent)) {
this.getRecursiveHierarchie(grLocation.getValue('parent'), resultObject);
}
}
}
},
type: 'LocationHelperAjax'
});
OnChange Client Script on Space field:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('LocationHelperAjax');
ga.addParam('sysparm_name', 'getLocationHierarchie');
ga.addParam('sysparm_space', newValue);
ga.getXML(callback);
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
answer = JSON.parse(answer);
var building = answer['building'];
var floor = answer['floor'];
g_form.setValue('u_building', building.sys_id, building.display_value);
g_form.setValue('u_floor', floor['sys_id'], floor['display_value']);
g_form.setValue('location', g_form.getValue('u_space'));
}
}
The OOTB Location Type for Buildings (choice value) should be renamed because it contains special characters and so the object operation could struggle. Hope that helps.
If this answer you initial question, please mark this answer as correct.
Greets
Daniel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2023 06:26 AM
Thanks so much Daniel - I will give this a try, this looks great.
Can I just confirm/clarify - what do you mean by 'only works if you rename OOTB Building choice'?
Do I need to do something here with Building ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2023 08:53 AM - edited 06-09-2023 09:08 AM
@WazzaJC The choice value of location type buildings is OOTB not the best string combination for such logic, because it includes a slash and special characters and this can cause some strange behaviour, but I didn't tested it with that. Maybe create your own Location Type for Buildings like I did. If you find my solution answers your question please mark it as correct. Have fun with the code. Greets Daniel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2023 02:14 AM
Hi Daniel - still trying to get this to work on my Client's Instance, I believe I am close.
Can I double check with you - in the Script Include - are you sure on line 5 of your script - is it not meant to be 'u_space' for my user case - as I have it below, not 'space' as you have it ?
var space = this.getParameter('sysparm_u_space');
Also - when I initially save/submit this Script Include - it will not let me submit without choosing an Access Control Role Name - which Role Name do I need to select so that the script works across whoever needs to use it, or how do I bypass this step Access Control step?
Many thanks again.