- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 07:03 AM
I am aware of a way to populate a single-line text variable using a catalog client script and SI. However, is there a way to populate a drop-down choice dynamically using the same method? We are wanting to eventually bring in data using an API and populate a drop-down on the fly with a list of courses, when a user logs-in. Since a drop-down uses a list of multiple choices manually entered when the variable is created, I wasn't sure if this data could be populated dynamically or not.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 07:49 AM
@appstorm You can build the choices for your drop-down/select box on the fly using an onLoad Client script and GlideAjax call to a server side script include. You can use the following code to add Option in the client script.
g_form.addOption('priority', '6', '6 - Really Low');
Here is the link to official documentation https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/client/c_GlideFormAPI#r_GF-AddO...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 05:26 AM - edited 08-21-2024 05:27 AM
@appstorm Here is the revised code from my side which should work.
//Client script
function onLoad() {
// function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// if (isLoading || newValue === '') {
// return;
//}
// var user = g_form.getValue('u_user');
var user = g_user.userID;
//Call script include
var ga = new GlideAjax('global.sampleUtils'); //Scriptinclude
ga.addParam('sysparm_name', 'getUserDetails'); //Method
ga.addParam('userId',user); //Parameters
ga.getXMLAnswer(getResponse);
function getResponse(response){
console.log("TESTING " + response);
var res = JSON.parse(response);
console.log("TESTING " + res.mobile_phone.toString());
g_form.setValue('u_test_string',res.mobile_phone.toString());
}
}
Here is the script include.
var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function(){
gs.addInfoMessage('script include triggered');
var userId = this.getParameter('userId');
gs.addInfoMessage('user scr--'+userId);
var obj = {};
var grSysUser = new GlideRecord('sys_user');
if (grSysUser.get(userId)) {
obj.mobile_phone = grSysUser.getValue('mobile_phone');
obj.email = grSysUser.getValue('email');
// obj.user_id = grSysUser.getValue('user_id');
}
gs.addInfoMessage('obj '+JSON.stringify(obj));
return JSON.stringify(obj);
},
type: 'sampleUtils'
});
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 12:50 PM
Thank you! I am getting a "Unhandled exception in GlideAjax" for the CS. I have a reference field (u_user) that auto-loads the ID for the logged-in user. In theory, the select box field (mobile_phone) should then display data on page-load, correct?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 06:40 PM
Ideally, yes. Could you please share the CS and SI code once again so that I can check the possible source of the error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 07:16 AM
SI:
var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function(){
gs.addInfoMessage('script include triggered');
var userId = this.getParameter('userId');
gs.addInfoMessage('user scr--'+userId);
var obj = {};
var grSysUser = new GlideRecord('sys_user');
if (grSysUser.get(userId)) {
obj.mobile_phone = grSysUser.getValue('mobile_phone');
obj.email = grSysUser.getValue('email');
// obj.user_id = grSysUser.getValue('user_id');
}
gs.addInfoMessage('obj '+JSON.stringify(obj));
return JSON.stringify(obj);
},
type: 'sampleUtils'
});
CS:
function onLoad() {
// function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// if (isLoading || newValue === '') {
// return;
//}
// var user = g_form.getValue('u_user');
var user = g_user.userID;
//Call script include
var ga = new GlideAjax('global.sampleUtils'); //Scriptinclude
ga.addParam('sysparm_name', 'getUserDetails'); //Method
ga.addParam('userId',user); //Parameters
ga.getXMLAnswer(getResponse);
function getResponse(response){
console.log("TESTING " + response);
var res = JSON.parse(response);
console.log("TESTING " + res.mobile_phone.toString());
g_form.setValue('u_test_string',res.mobile_phone.toString());
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 07:39 AM
@appstorm Can you try the following and see if it works.
SI:
var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function(){
gs.addInfoMessage('script include triggered');
var userId = this.getParameter('sysparm_userId');
gs.addInfoMessage('user scr--'+userId);
var obj = {};
var grSysUser = new GlideRecord('sys_user');
if (grSysUser.get(userId)) {
obj.mobile_phone = grSysUser.getValue('mobile_phone');
obj.email = grSysUser.getValue('email');
// obj.user_id = grSysUser.getValue('user_id');
}
gs.addInfoMessage('obj '+JSON.stringify(obj));
return JSON.stringify(obj);
},
type: 'sampleUtils'
});
CS:
function onLoad() {
// function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// if (isLoading || newValue === '') {
// return;
//}
// var user = g_form.getValue('u_user');
var user = g_user.userID;
//Call script include
var ga = new GlideAjax('global.sampleUtils'); //Scriptinclude
ga.addParam('sysparm_name', 'getUserDetails'); //Method
ga.addParam('sysparm_userId',user); //Parameters
ga.getXMLAnswer(getResponse);
function getResponse(response){
console.log("TESTING " + response);
var res = JSON.parse(response);
console.log("TESTING " + res.mobile_phone.toString());
g_form.setValue('u_test_string',res.mobile_phone.toString());
}
}
Hope this works.