I am trying auto populate user location on load of a record producer . using this script but it is not populating

Rahul Singh3
Tera Contributor

I am using this script for the same :

function onLoad() {
var id = g_form.getValue('caller_id');
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', id);
user.query();
if (user.next()) {
g_form.setValue('location', user.location);

}
}

Also trying to change the location is caller is changed in the same record producer and using this script.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == '' || newValue == null) {
g_form.setValue('location', '');
return;
}
if (!g_form.hasField('location'))
return;
var caller = g_form.getReference('caller_id', setLocation);
}

function setLocation(caller) {
if (caller)
g_form.setValue('location', caller.location);
}

 

both are not working. could anyone suggest why?

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use default value in the variable

javascript: gs.getUser().getLocation();

Update onChange as this

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if (newValue == '') {
		g_form.clearValue('location');
		return;
	}
	if(oldValue != newValue)
		var caller = g_form.getReference('caller_id', setLocation);
}

function setLocation(caller) {
	if (caller)
		g_form.setValue('location', caller.location);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

this location does not seems to work
find_real_file.png

Rahul,

Just want to make sure. Is "Caller" a reference to sys_user table and Location a reference to cmn_location table?

Following is my client script.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    if (newValue == '') {
        g_form.clearValue('location');
        return;
    }
    if (oldValue != newValue) {
        g_form.getReference('caller_id', setLocation);
    }

    function setLocation(caller) {
        if (caller)
            g_form.setValue('location', caller.location);
    }
}

Execution result. Location is being filled in.

find_real_file.png

Just to make sure, if viewing in Service Portal, is "UI Type" set to "All"?

find_real_file.png

Hi,

it should work provided the logged in user has location

I believe you want logged in user's location to be populated

Then if caller is changed the location can be fetched using onChange client script

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader