Multi Row variable set values store in an object

pawan2
Tera Expert

Hi All,

Hope you are all well!. I need your valuable thought about how to store multi row variable values in an object. Currently I'm developing a rest API GET method to retrieve data from ServiceNow. Below is the high-level scripted REST API GET method I'm developing. However I could retrieve variable values, But I'm getting undefined error when I trying to retrieve multi row variable set values. Appreciate your help here.

var g = new GlideRecord('sc_task');

g.get(SYS_ID OF THE SCTASK);

scTask.query();

while(g.next()) {

var obj = {};

obj.app= obj.application_name= g.variables.app_id.getDisplayValue();//working fine

obj.account= g.variables.account.toString();//working fine

obj.multiCategory=g.variables.multiRowVariableSet1.user_name; // This part is not working. Multi row variable set name is multiRowVariableSet1 and the variable name is user_name. I need to store this multi row variable values in the same obj I've created here.

result.push(obj);

 

 

Thanks & Regards,

Pawan

1 ACCEPTED SOLUTION

sys_id will only select 1 record so the other conditions shouldn't be necessary.

The script will return the following error when there is no mrvs in the ritm with the name "multirowvariableset1".

Evaluator: com.glide.script.RhinoEcmaError: Unexpected token: u

To avoid a script error, use try-catch block.

var sctaskSysid;
var query = 'sys_id=' + sctaskSysid + '^request_item.cat_item=32126h5689kht65t896431g76j9ld432^variables.3f79j5639827hn936068f36k8f6n9753=No'; // paste encoded query here

var result = [];
var grSctask = new GlideRecord('sc_task');
grSctask.addEncodedQuery(query);
grSctask.query();

while (grSctask.next()) {
    try {
        var requestItem = grSctask.request_item; // get request item for the task
        var mrvs = requestItem.variables.multirowvariableset1; // to get content of mrvs
        var jsonObj = JSON.parse(mrvs);
        var userList = [];
        for (var i = 0; i < jsonObj.length; i++) {
            userList.push(jsonObj[i].user_name.toString());
        }
        var obj = {};
        //obj.app= obj.application_name= grSctaskgr.variables.app_id.getDisplayValue();//working fine
        //obj.account= grSctask.variables.account.toString();//working fine
        obj.multiCategory = userList.join(',').toString();
        result.push(obj);
        gs.info(result[0].multiCategory);
    } catch (e) {}
}

 

View solution in original post

21 REPLIES 21

Please provide the query statement that's being used to test.

It works in my PDI so it's probably a problem with the query statement being used.

Hi Hitoshi. This is the one I tried. Thank you!

 

//var sctaskSysid = '7457jg579kld25gn80468317jg8042r8'; // example to dynamically generate query

var sctaskSysid; var query = 'sys_id=' + sctaskSysid + '^request_item.cat_item=32126h5689kht65t896431g76j9ld432^variables.3f79j5639827hn936068f36k8f6n9753=No'; // paste encoded query here

var result = [];

var grSctask= new GlideRecord('sc_task');

grSctask.addEncodedQuery(query);

grSctask.query();

while (grSctask.next()) {

var requestItem= grSctask.request_item;  // get request item for the task

var mrvs = requestItem.variables.multirowvariableset1; // to get content of mrvs

var jsonObj = JSON.parse(mrvs);

var userList = [];

for (var i=0; i< jsonObj.length; i++) { userList.push(jsonObj[i].user_name.toString());

}

var obj = {};

//obj.app= obj.application_name= grSctaskgr.variables.app_id.getDisplayValue();//working fine

//obj.account= grSctask.variables.account.toString();//working fine

obj.multiCategory = userList.join(',').toString();

result.push(obj);

gs.info(result[0].multiCategory);

}

sys_id will only select 1 record so the other conditions shouldn't be necessary.

The script will return the following error when there is no mrvs in the ritm with the name "multirowvariableset1".

Evaluator: com.glide.script.RhinoEcmaError: Unexpected token: u

To avoid a script error, use try-catch block.

var sctaskSysid;
var query = 'sys_id=' + sctaskSysid + '^request_item.cat_item=32126h5689kht65t896431g76j9ld432^variables.3f79j5639827hn936068f36k8f6n9753=No'; // paste encoded query here

var result = [];
var grSctask = new GlideRecord('sc_task');
grSctask.addEncodedQuery(query);
grSctask.query();

while (grSctask.next()) {
    try {
        var requestItem = grSctask.request_item; // get request item for the task
        var mrvs = requestItem.variables.multirowvariableset1; // to get content of mrvs
        var jsonObj = JSON.parse(mrvs);
        var userList = [];
        for (var i = 0; i < jsonObj.length; i++) {
            userList.push(jsonObj[i].user_name.toString());
        }
        var obj = {};
        //obj.app= obj.application_name= grSctaskgr.variables.app_id.getDisplayValue();//working fine
        //obj.account= grSctask.variables.account.toString();//working fine
        obj.multiCategory = userList.join(',').toString();
        result.push(obj);
        gs.info(result[0].multiCategory);
    } catch (e) {}
}

 

Hi Hitoshi. Thanks for continuesly supporting. Much appreciated. I can confirm script is running without any errors when added the try catch. But it doesn't return any values which means no sctasks getting return. Could you help me again please.

 

Thanks in advance! 

It's the query conditions. It's not matching any records or matching a record that doesn't have mrvs named "multirowvariableset1".

Check all records that should match and check in all these records if there is a mrvs named "multirowvariableset1".