How to get location field autopopulate on service catalog based on request_for field

Vineela1
Tera Contributor

Hi Team,

Please suggest , how to get "location" field autopopulate in Service catalog form based on "Request for" field.

 

Thanks and Regards,

 

Vineela

1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi @Vineela1 ,

If your location variable is reference type which is referenced to the cmn_location table.

Then no need script

1.On Catalog Data Lookup Definitions create new and matcher table as user table.

 

Screenshot (308).png

2. On Catalog Matcher Variable Definitions create new and select requested for variable and matcher table field as sysid.

Screenshot (309).png

3. On catalog Setter Variable Definitions Source is location variable and matcher table field is Location.Screenshot (310).png 

if it is a single line text type. Try below code

1. Create on change catalog client script on requested for variable

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('global.setlocation'); //script include name
    ga.addParam('sysparm_name', 'getlocation'); //function name
    ga.addParam('sysparm_usrsysid', newValue); //passing user sysid to server
    ga.getXMLAnswer(setuserlocation); //callback funtion
    function setuserlocation(response) {
        g_form.setValue('location', response); //give your location variable name
    }
}

 

2. Script include

 

var setlocation = Class.create();
setlocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getlocation: function() {
        var sysid = this.getParameter('sysparm_usrsysid');
        var grusr = new GlideRecord("sys_user");
        grusr.addQuery('sys_id', sysid);
        grusr.query();
        if (grusr.next()) {
			var usrlocation=grusr.location;
            var grloc = new GlideRecord("cmn_location");
            grloc.addQuery('sys_id', usrlocation);
            grloc.query();
            if (grloc.next()) {
                return grloc.name;
            }
        }
    },
    type: 'setlocation'
});

 

Screenshot:

Screenshot (311).png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

3 REPLIES 3

RaghavSh
Kilo Patron

 

var user = g_form.getReference('requested_for',callback);
function callback(user)
{
g_form.setValue('location',user.location);
}

 

Please verify the backend names of variables, also this should be written on  change of requested for variable.

If you want this to work on load as well, remove the isLoading from onChange script.

 


Raghav
MVP 2023

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Write a onchange client scrip on request for field with the below script:-

 

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

	g_form.getReference('requestforvariablename', callBack); 
	function callBack(user){
		g_form.setValue('locationvariable',user.location);
	}
}

 

Please mark my answer as correct based on Impact.

Pavankumar_1
Mega Patron

Hi @Vineela1 ,

If your location variable is reference type which is referenced to the cmn_location table.

Then no need script

1.On Catalog Data Lookup Definitions create new and matcher table as user table.

 

Screenshot (308).png

2. On Catalog Matcher Variable Definitions create new and select requested for variable and matcher table field as sysid.

Screenshot (309).png

3. On catalog Setter Variable Definitions Source is location variable and matcher table field is Location.Screenshot (310).png 

if it is a single line text type. Try below code

1. Create on change catalog client script on requested for variable

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('global.setlocation'); //script include name
    ga.addParam('sysparm_name', 'getlocation'); //function name
    ga.addParam('sysparm_usrsysid', newValue); //passing user sysid to server
    ga.getXMLAnswer(setuserlocation); //callback funtion
    function setuserlocation(response) {
        g_form.setValue('location', response); //give your location variable name
    }
}

 

2. Script include

 

var setlocation = Class.create();
setlocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getlocation: function() {
        var sysid = this.getParameter('sysparm_usrsysid');
        var grusr = new GlideRecord("sys_user");
        grusr.addQuery('sys_id', sysid);
        grusr.query();
        if (grusr.next()) {
			var usrlocation=grusr.location;
            var grloc = new GlideRecord("cmn_location");
            grloc.addQuery('sys_id', usrlocation);
            grloc.query();
            if (grloc.next()) {
                return grloc.name;
            }
        }
    },
    type: 'setlocation'
});

 

Screenshot:

Screenshot (311).png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar