GlideappVariablePoolQuestionSet not returning all variables question and values

Waseem4
Tera Expert

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);

 

 

find_real_file.png

 

find_real_file.png

1 ACCEPTED SOLUTION

Waseem4
Tera Expert

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()
					
				};

View solution in original post

11 REPLIES 11

Hi, 

 

Yes it does. When running this in background script, the values are returned. 

So what do I need to change in my code?

 

 

Do any values return or none in postman?

May be a Read ACL on sc_item_option is blocking your web service user.

 

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Hey, 

So, some values do return. But other values do not return. e.g.

find_real_file.png

I will have a look at the ACL. Maybe it's something to do with the type of variable? 

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]

 

 

 

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Waseem4
Tera Expert

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()
					
				};