I want a fetch all the variable for the RITM request and i have used the REST scripted API but i am getting the sys_id for the some of the fields but i do not want sysid beacuse end user will not under stand, Can you advise how to get the value .
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var variableName, i, ritmVariables, ritmNumber, grRitm, variablesObject, ritmNumbers;
var responseObj = {}; //The object that should ultimately be returned.
//Set ritmNumber from header value, convert to an array, and trim each element.
ritmNumbers = trimAndSplit(request.getHeader('ritm'), ','); //Calling custom script include.
//ritmNumbers = request.getHeader('ritm').split(','); //<--Commented out, but you can enable it if you don't have the trimAndSplit script include.
if (!ritmNumbers) {
//If ritm header was not set or was malformed, return an error.
response.setStatus(417);
return 'Unable to process request. Request header \'ritm\' not defined. ' +
'Please define a request header called \'ritm\' with the value set to the ticket number for the RITM you\'d like to retrieve the variables for. Value provided for RITM: ' +
ritmNumbers;
}
grRitm = new GlideRecord('sc_req_item');
//For each RITM in the ritmNumbers array...
for (i = 0; i < ritmNumbers.length; i++) {
//Get the RITM number
ritmNumber = ritmNumbers[i];
//Get the GlideRecord for the RITM, or throw an error if we can't.
if (!grRitm.get('number', ritmNumber)) { //If we can't find the RITM...
response.setStatus(417); //set the status to indicate a bad argument
response.setHeader(ritmNumber,
'Unable to locate requested ritm: ' + ritmNumber); //add a header to indicate which RITM was bad.
gs.logError(
'Scripted REST API ritm_vars_api unable to process request. RITM not found: ' +
ritmNumber); //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.
ritmVariables = grRitm.variables;
//Declare a fresh object each loop.
//This will be made to contain all of the variables and variable values for the RITM we're iterating over,
//Then it will be pushed into the responseObj object, and reset for the next RITM on the next iteration.
variablesObject = {};
//Set the 'number' property on the variables object to the current ritm number. This will also be the
variablesObject["number"] = ritmNumber;
//Iterate over ritmVariables, looping through each one as v.
//This is necessary because the "gr.variables" is not a standard JS object, and cannot be mapped.
for (variableName in ritmVariables) {
//NOTE: If you want to return all variables on the catalog item associated with the RITM, remove the second condition in the IF block below.
//With the second condition, this will only show variables that have been populated.
if (ritmVariables.hasOwnProperty(variableName) && ritmVariables[variableName]) { //Make sure the property exists and isn't null or unknown.
variableName = variableName.toString(); //Make sure we're all proper strings here.
//pushing the variable into variablesObject, which will be copied into responseObject along with a version of variablesObject for each of the RITMs.
variablesObject[variableName] = ritmVariables[variableName].toString();
}
}
//Call addObjToObj function to add variablesObject to responseObj with the key set to the current RITM number, so it can be accessed as 'responseObj["RITM0123456"]'.
//NOTE: If we didn't use addObjToObj, we'd run into this problem where objects are linked rather than copied, when added to other objects in javascript.
responseObj = addObjToObj(responseObj, variablesObject, ritmNumber);
}
return responseObj; //Returning the variables and their values. Returning an object here will set the response "body" to the JSON equivalent of this object.
//Helper function below
function addObjToObj(parent, child, name) {
parent[name] = child;
return parent; //Note: Does not break pass-by-reference, because we're declaring a new object on each loop on line 39 above.
}
})(request, response);
script include :
script include name : trimAndSplit
/**
* Converts a string to an array, much like .split(), except that it trims each element of leading and trailing whitespace.
* @Param inputString {string} The string that should be split and trimmed * @Param [token=,] {string} The character to split on (usually ','). Default value is a comma character. * @returns {Array} An array of elements that have each been trimmed of whitespace.
*/
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;
}