- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2022 04:29 AM
Good Afternoon,
We need to call an REST API from the portal to request information in an external system for that we've created the following Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == 'true') {
return;
}
var colleague = g_form.getValue("colleague");
var gaj = new GlideAjax('generate_id');
gaj.addParam('sysparm_name', 'getID');
gaj.addParam('sysparm_user', colleague);
gaj.getXMLAnswer(function(response){
var myJSON = JSON.parse(response);
var ID = myJSON["PossibleUsernames[1].Username"];
g_form.addInfoMessage(ID);
});
As you can see this Client Script calls a Script Include called generate_id
This script include looks like this:
var generate_id = Class.create();
generate_id.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getID: function() {
var user = {};
var gr = GlideRecord("sys_user");
if (gr.get(this.getParameter("sysparm_user"))) {
user.firstName = gr.getValue("first_name");
user.lastName = gr.getValue("last_name");
user.mdmKey = gr.getValue("u_mdm_key");
}
var message = new sn_ws.RESTMessageV2("Username_v1", "/username/generate");
message.setStringParameterNoEscape("firstname", user.firstName);
message.setStringParameterNoEscape("lastname", user.lastName);
message.setStringParameterNoEscape("mdmkey", user.mdmKey);
var response = message.execute();
var responseBody = response.getBody();
return JSON.stringify(responseBody);
},
type: "generate_id"
});
The response we get in the return looks like the following:
{"PossibleUsernames":[{"Username":"pepig","Priority":1},{"Username":"peppi","Priority":2},{"Username":"peppp","Priority":3},{"Username":"peppa","Priority":4}]}
I'm looking for a way to extract the Usernames such as pepig, peppi, pepp, peppa and use the in my Catalog Client Script so i can use them in a g_form.addOption() or g_form.setValue().
Solved! Go to Solution.
- Labels:
-
Integrations

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2022 05:10 AM
Hi,
You can update client script like below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == 'true') {
return;
}
var colleague = g_form.getValue("colleague");
var gaj = new GlideAjax('generate_id');
gaj.addParam('sysparm_name', 'getID');
gaj.addParam('sysparm_user', colleague);
gaj.getXMLAnswer(function(response){
var myJSON = JSON.parse(response);
var userNames = myJSON.PossibleUsernames;
for(var i=0; i<userNames.length;i++){
g_form.addInfoMessage(userNames[i].Username);
//to add option use below logic
g_form.addOption('variable_name', userNames[i].Username,userNames[i].Username);
}
});
Thanks,
Anil Lande
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2022 05:10 AM
Hi,
You can update client script like below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == 'true') {
return;
}
var colleague = g_form.getValue("colleague");
var gaj = new GlideAjax('generate_id');
gaj.addParam('sysparm_name', 'getID');
gaj.addParam('sysparm_user', colleague);
gaj.getXMLAnswer(function(response){
var myJSON = JSON.parse(response);
var userNames = myJSON.PossibleUsernames;
for(var i=0; i<userNames.length;i++){
g_form.addInfoMessage(userNames[i].Username);
//to add option use below logic
g_form.addOption('variable_name', userNames[i].Username,userNames[i].Username);
}
});
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2022 05:40 AM
Hi Anil,
My new script will then be:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == 'true') {
return;
}
var colleague = g_form.getValue("colleague");
var gaj = new GlideAjax('generate_id');
gaj.addParam('sysparm_name', 'getID');
gaj.addParam('sysparm_user', colleague);
gaj.getXMLAnswer(function(response) {
var myJSON = JSON.parse(response);
var userNames = myJSON.PossibleUsernames;
for (var i = 0; i < userNames.length; i++) {
g_form.addInfoMessage(userNames[i].Username);
//to add option use below logic
g_form.addOption('proposed_user_id', userNames[i].Username, userNames[i].Username);
}
});
}
When i do this i get Unhandled exception in GlideAjax. Can you see what is wrong?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2022 06:00 AM
Hi,
Can you please put some logs in Script include and see what it is returning.
Also put Info message in Client script to check response.
If you receive correct response the these changes would work.
I tried returning your json from SI and printing the Names.
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2022 06:10 AM
For some reason my response is different this time it has slashes
"{\"PossibleUsernames\":[{\"Username\":\"pepig\",\"Priority\":1},{\"Username\":\"peppi\",\"Priority\":2},{\"Username\":\"peppp\",\"Priority\":3},{\"Username\":\"peppa\",\"Priority\":4}]}"