Variables in multi-row to be sent via REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2023 07:26 AM - edited 03-15-2023 11:07 PM
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';
}
---------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2023 08:19 AM - edited 03-15-2023 08:22 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2023 11:08 PM
Thanks for the inputs. could you look through the script updated in the post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2023 11:13 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2023 11:30 PM
how is the json looking after your script?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader