We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Variables in multi-row to be sent via REST API

Not applicable

UPDATE : There is a 'Variable Set' on a request catalog which only includes a multi row table containing 5 fields that will have 5 rows of info in it. looking to send those variable values to a third party application using REST API. 

Need help is below script which will be added in the workflow to send the values of up to 5 rows (could be 1 to 5 rows ).Any help is appreciated.

Below has been tested successfully in the Rest API HTTP Method test.

{
"input_vars": {
      "Template": [{
           "number": "1",
           "perh": "1",
           "ear": "1",
           "file": "1",
           "day": "1",

},

{
           "number": "2",
           "perh": "2",
           "ear": "2",
           "file": "2",
           "day": "2",

}

--------------------------------------------------------

 

var gr = new GlideRecord(current.sys_id);
var mrvs = new GlideMultipleRowVariableSet().get(current, 'MultiRow_VARSET');
var variables = [];
for (var i = 0; i < mrvs.getSize(); i++) {
var row = mrvs.getRow(i);
var obj = {};
obj.var1 = row.number.toString();
obj.var2 = row.perh.toString();
obj.var3 = row.ear.toString();
obj.var4 = row.file.toString();
obj.var5 = row.day.toString();

variables.push(obj);
gs.log('TEST ' + mrvs + 'Rows == ' + rowCount);
gs.log("variables " + obj);
}
 var json = JSON.stringify(obj);
var rm, response, status, responseBody, errorMsg;
try {

var r = new sn_ws.RESTMessageV2('Restcall', 'POST');
// r.setStringParameterNoEscape('messageBody', JSON.stringify(Data));
r.setRequestBody(json);
response = r.execute();
status = response.getStatusCode();
responseBody = response.getBody();
errorMsg = response.haveError() ? response.getErrorMessage() : '';
} catch (ex) {
errorMsg = ex.getMessage();
status = '500';

---------------------------------------------------------------------

 

 

 

 

amahmed_0-1678890056848.png

 

8 REPLIES 8

phil bool
Tera Contributor

The Multi-Row variable set sits in the record's 'Variables' object.   So, once you've got the record as a GlideRecord, you can dot-walk through its variables object to get the value in the MRVS.

For example, if you had a record on the request item table with a variables object that contained a multi-row variable set called 'mrvs', you could get the content as follows:

var gr = new GlideRecord('sc_req_item');

gr.get('[sys_id]');
var data = JSON.parse(gr.variables.mrvs);  

Once you've got the values you need in a JSON object, you can use the values to populate an object to send out via your API. 

Not applicable

Thanks for the inputs. could you look through the script updated in the post.

Not applicable

@Ankur Bawiskar appreciate if you can help look at the script. Thanks

 

 

Ankur Bawiskar
Tera Patron

@Community Alums 

how is the json looking after your script?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader