- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2020 08:16 AM
Hi All,
I am trying to fetch all the variables on RITM/SCTASK and I found this article
Which helped me in getting the variables using a Scripted REST api to get all the variables. But this Scripted API is not fetching Multi-row variable set which is on RITM/SCTASK. Can some one please help me in modifying the code so that I can fetch Multi-Row variable set as well.
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2020 01:53 PM
Hi Asif,
Thanks for you input/suggestion and finally it worked.
here is the final code how it looks.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var variableName, i, sctaskVariables, sctaskNumber, grSCTask, variablesObject, sctaskNumbers;
var objApproval = {}; //The object that should ultimately be returned.
//Set sctaskNumber from header value, convert to an array, and trim each element.
sctaskNumbers = trimAndSplit(request.getHeader('sctask'), ','); //Calling custom script include.
//sctaskNumbers = request.getHeader('ritm').split(','); //<--Commented out, but you can enable it if you don't have the trimAndSplit script include.
if (!sctaskNumbers) {
//If sctask header was not set or was malformed, return an error.
response.setStatus(417);
return 'Unable to process request. Request header \'sctask\' not defined. ' +
'Please define a request header called \'sctask\' with the value set to the ticket number for the SCTASK you\'d like to retrieve the variables for. Value provided for SCTASK: ' + sctaskNumbers;
}
grSCTask = new GlideRecord('sc_task');
//For each SCTASK in the sctaskNumbers array...
for (i = 0; i < sctaskNumbers.length; i++) {
//Get the SCTASK number
sctaskNumber = sctaskNumbers[i];
//Get the GlideRecord for the SCTASK, or throw an error if we can't.
if (!grSCTask.get('number', sctaskNumber)) { //If we can't find the RITM...
response.setStatus(417); //set the status to indicate a bad argument
response.setHeader(sctaskNumber,
'Unable to locate Catalog Task: ' + sctaskNumber); //add a header to indicate which RITM was bad.
gs.logError(
'Scripted REST API ritm_vars_api unable to process request. SCTASK not found: ' +
sctaskNumber); //Log an error so the admin knows what happens if the requestor asks.
continue; //Continue the loop with the next RITM in case there's anything valid we *can* return.
}
//Get the object containing the catalog variables.
sctaskVariables = grSCTask.variables;
var mrvs = JSON.parse(grSCTask.variables.test_copy);
for (var j = 0; j < mrvs.length; j++) {
objApproval['Row'+j] = mrvs[j];// Build the object with group and approval order
}
}
//responseObj = addObjToObj(mrvs);
gs.log('FInal OBJ == ' + JSON.stringify(objApproval));
return objApproval;
function trimAndSplit(inputString, token) {
var i, inputArr;
var resultArray = [];
//Give token a default value if not specified
token = (!token) ? ',' : token;
if (!inputString || typeof inputString !== 'string') {
gs.logError(
'trimAndSplit function received an invalid argument in the first argument. The argument received was: ' + inputString + '. The only acceptable first argument for trimAndSplit, is a string.');
return [];
}
if (inputString.indexOf(token) < 0) {
//Return the original string in an array, because no instances of the token were found.
return [inputString];
}
//convert the input string to an array, splitting on the provided token (usually ',')
inputArr = inputString.split(token);
for (i = 0; i < inputArr.length; i++) {
//Trim each element in the split string array, then push it to the return array.
resultArray.push(inputArr[i].trim());
}
//return the trimmed and split array.
return resultArray;
}
})(request, response);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2020 10:30 AM
Hi,
Here is the updated code. The response obj was eing overridden in the loop. Hence at the end of the loop, you are getting last object.
Either push the object to an array inside loop and then access the array after the loop or do the parsing inside the loop for each row.
var mrvs = grRitm.variables.test_copy;
var rowCount = mrvs.getRowCount();
gs.log('Naveen ' +mrvs+'Rows == '+rowCount);
for (var j = 0; j < rowCount; j++) {
var row = mrvs.getRow(j);
var var1 = row.test_1;
var var2 = row.test_2;
responseObj ={
"test_1":row.test_1,
"test_2":row.test_2,
};
gs.log('respose OBJ == '+responseObj);
}
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2020 01:53 PM
Hi Asif,
Thanks for you input/suggestion and finally it worked.
here is the final code how it looks.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var variableName, i, sctaskVariables, sctaskNumber, grSCTask, variablesObject, sctaskNumbers;
var objApproval = {}; //The object that should ultimately be returned.
//Set sctaskNumber from header value, convert to an array, and trim each element.
sctaskNumbers = trimAndSplit(request.getHeader('sctask'), ','); //Calling custom script include.
//sctaskNumbers = request.getHeader('ritm').split(','); //<--Commented out, but you can enable it if you don't have the trimAndSplit script include.
if (!sctaskNumbers) {
//If sctask header was not set or was malformed, return an error.
response.setStatus(417);
return 'Unable to process request. Request header \'sctask\' not defined. ' +
'Please define a request header called \'sctask\' with the value set to the ticket number for the SCTASK you\'d like to retrieve the variables for. Value provided for SCTASK: ' + sctaskNumbers;
}
grSCTask = new GlideRecord('sc_task');
//For each SCTASK in the sctaskNumbers array...
for (i = 0; i < sctaskNumbers.length; i++) {
//Get the SCTASK number
sctaskNumber = sctaskNumbers[i];
//Get the GlideRecord for the SCTASK, or throw an error if we can't.
if (!grSCTask.get('number', sctaskNumber)) { //If we can't find the RITM...
response.setStatus(417); //set the status to indicate a bad argument
response.setHeader(sctaskNumber,
'Unable to locate Catalog Task: ' + sctaskNumber); //add a header to indicate which RITM was bad.
gs.logError(
'Scripted REST API ritm_vars_api unable to process request. SCTASK not found: ' +
sctaskNumber); //Log an error so the admin knows what happens if the requestor asks.
continue; //Continue the loop with the next RITM in case there's anything valid we *can* return.
}
//Get the object containing the catalog variables.
sctaskVariables = grSCTask.variables;
var mrvs = JSON.parse(grSCTask.variables.test_copy);
for (var j = 0; j < mrvs.length; j++) {
objApproval['Row'+j] = mrvs[j];// Build the object with group and approval order
}
}
//responseObj = addObjToObj(mrvs);
gs.log('FInal OBJ == ' + JSON.stringify(objApproval));
return objApproval;
function trimAndSplit(inputString, token) {
var i, inputArr;
var resultArray = [];
//Give token a default value if not specified
token = (!token) ? ',' : token;
if (!inputString || typeof inputString !== 'string') {
gs.logError(
'trimAndSplit function received an invalid argument in the first argument. The argument received was: ' + inputString + '. The only acceptable first argument for trimAndSplit, is a string.');
return [];
}
if (inputString.indexOf(token) < 0) {
//Return the original string in an array, because no instances of the token were found.
return [inputString];
}
//convert the input string to an array, splitting on the provided token (usually ',')
inputArr = inputString.split(token);
for (i = 0; i < inputArr.length; i++) {
//Trim each element in the split string array, then push it to the return array.
resultArray.push(inputArr[i].trim());
}
//return the trimmed and split array.
return resultArray;
}
})(request, response);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2020 11:47 PM
Great.
Could you mark my comment as a correct answer which leads you to the final solution.
Regards,
Asif
2020 ServiceNow Community MVP