How Can I autopopulate 'Location' Reference field with same value as shown in Space Reference field

WazzaJC
Tera Expert

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.

20 REPLIES 20

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

Hi @WazzaJC ,

 

This location setup as example.

 

DanielBorkowi1_0-1686315967831.png

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'
});

 

 

 

 

DanielBorkowi1_1-1686550669589.png

 

 

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'));

    }
 }

 

 

 

 

DanielBorkowi1_0-1686550641281.png

 

 

 

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

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 ?

@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

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.