- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 11:18 AM
Hi,
I have an issue with a scripted rest api which uses GlideappVariablePoolQuestionset to return all variable data.
The purpose of this rest api is to allow external users to call this api (passing in sctask number via query params) which will return all populated variables questions and answers.
Unfortunately this does not return all the populated variables and values. Please see below for the scripted rest api.
From the screen shot you can see the variables have been populated but it does not return the data. For some variables it does return but for other it does not.
Any help will be appreciated.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var parent_result = [];
var item_variable = [];
var obj = {};
var variableName="";
var params = request.queryParams;
var tsk_num = params.sc_task_sys_id.toString();
var usrname = gs.getProperty("CatalogTaskScriptedAPIUser");
var psswrd = gs.getProperty("CatalogTaskScriptedAPIUsrPass");
var endpt = gs.getProperty("CatalogTaskScriptedAPIEndpt");
var req = new sn_ws.RESTMessageV2();
req.setHttpMethod('get');
req.setBasicAuth(usrname.toString(), psswrd.toString());
req.setEndpoint(endpt.toString());
req.setQueryParameter("sysparm_query", "number=" + tsk_num);
req.setQueryParameter("sysparm_display_value", "all");
var resp = req.execute();
var resp_bod = resp.getBody();
var sctsk = new GlideRecord("sc_task");
sctsk.addQuery("number", tsk_num);
sctsk.query();
if (sctsk.next()) {
var i = 0;
var grRitm = new GlideRecord("sc_req_item");
grRitm.addQuery("sys_id", sctsk.request_item);
grRitm.query();
if (grRitm.next()) {
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(grRitm.sys_id); // requested item sys_id
set.load();
var vs = set.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
var vname = vs.get(i).getLabel();
obj[vname] = {
"display_value": vs.get(i).getDisplayValue(),
"value": vs.get(i).getValue().toString()
};
}
var mRow = new global.GetMultiRowJsonforSCTaskAPI();
var mRowVal = mRow.intit(sctsk.request_item);
if(mRowVal!=JSON.stringify({}))
obj["MultiRow"]=JSON.parse(mRowVal);
}
}
//return result;
parent_result.push(JSON.parse(resp_bod));
parent_result.push(obj);
//if(mRowVal)
//parent_result.push(JSON.parse(mRowVal));
return parent_result;
})(request, response);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 11:38 AM
Issue is now resolved. I had to add an if condition to look for only populated display values.
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(grRitm.sys_id); // requested item sys_id
set.setTaskID(grRitm.sys_id);
set.load();
var vs = set.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
var vname = vs.get(i).getLabel().toString();
if(vs.get(i).getDisplayValue() != '') {
obj[vname] = {
"display_value": vs.get(i).getDisplayValue(),
"value": vs.get(i).getValue().toString()
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 02:02 PM
Hi,
Yes it does. When running this in background script, the values are returned.
So what do I need to change in my code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 02:05 PM
Do any values return or none in postman?
May be a Read ACL on sc_item_option is blocking your web service user.
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 02:21 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 02:31 PM
Or in the screenshot the question label is the same for multiple questions.
"What do you want to do?" and in the return object you are storing it as the key
obj[vname]
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 11:38 AM
Issue is now resolved. I had to add an if condition to look for only populated display values.
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(grRitm.sys_id); // requested item sys_id
set.setTaskID(grRitm.sys_id);
set.load();
var vs = set.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
var vname = vs.get(i).getLabel().toString();
if(vs.get(i).getDisplayValue() != '') {
obj[vname] = {
"display_value": vs.get(i).getDisplayValue(),
"value": vs.get(i).getValue().toString()
};