Auto-Populating Catalog Item Variables with Fields from User's Corresponding Location
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2023 01:33 PM - edited 03-14-2023 01:44 PM
Hi All,
I am relatively new to ServiceNow and have been tasked with building a new Catalog Item. On the Catalog item I have variables with names such as "district_name", "district_number", and "county_name". I basically want to fill in those corresponding variables automatically when a user loads the catalog item. In the screenshot is an example location record for a user showing the fields I need to gather from. For example if user opens catalog item, I want the user's district_name variable to automatically get "West Valley", the county_name to automatically get "Santa Clara", and the district_number to automatically get "22".
There is only one thing I was able to get working... I was able to set the default value setting for the district_name variable to: javascript:gs.getUser().getRecord().getDisplayValue('location');
However what I really need to prefill the all the variables based from the user's location, such as district_number ("u_school_id" field on the user's Location record) or the county_name ("parent" field on user's Location record)...
I searched around and it seems like the if I need to get fields from the user's corresponding location (dot-walk location), the suggested way to go about it is described on this post. I tried setting up a script include and onChange catalog client script based on that post...but for the life of me I cannot get it to work :(...
Below is my code for the script include and onChange catalog client script.
//Script Include
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord("sys_user");
gr.get(user);
var location_parent = gr.location.parent;
var location = gr.location;
var location_school_id = gr.location.u_school_id;
var response = {};
response.location_parent = location_parent;
response.location = location;
response.location_school_id = location_school_id;
return JSON.stringify(response);
},
type: 'u_userInfoAjax'
});
//Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
// Call the GA function
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getInfo");
ga.addParam('sysparm_user', g_form.getValue('requested_by'));
ga.getXMLAnswer(function(answer){
// Save the return to a global variable
var info = JSON.parse(answer);
g_form.setValue('county_name', info.location_parent);
g_form.setValue('district_name', info.location);
g_form.setValue('district_number', info.location_school_id);
});
}
I would truly appreciate any help with correcting the code for the script include and/or catalog client script as needed. I have spent hours and hours looking over the scripts and I cannot see the source of the issue.
Has anyone done something similar before? Can someone tell me if I am trying to go about this in the wrong manner? Not sure if it matters but after the variables mentioned above are pre-populated I want them to be set to "read-only".
Thanks for any help,
Chris