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

But your screenshot of SCTASK had a value for the question...right?

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

Hi, 

 

Because there were multiple questions with the same name, it was displaying the question which didn't have a value. So had to add an if condition to only display the variables that had values.