- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 03:17 AM
Hi Team,
Please suggest , how to get "location" field autopopulate in Service catalog form based on "Request for" field.
Thanks and Regards,
Vineela
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 04:21 AM
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.
2. On Catalog Matcher Variable Definitions create new and select requested for variable and matcher table field as sysid.
3. On catalog Setter Variable Definitions Source is location variable and matcher table field is Location.
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:
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 03:22 AM - edited 11-04-2022 03:23 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 03:32 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 04:21 AM
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.
2. On Catalog Matcher Variable Definitions create new and select requested for variable and matcher table field as sysid.
3. On catalog Setter Variable Definitions Source is location variable and matcher table field is Location.
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:
ServiceNow Community MVP 2024.
Thanks,
Pavankumar