How to create service account to call API.

niveditakumari
Mega Sage

Hi, 

 

How to create service account to call API. I need to create service account to call API an read all records from catalog item. 

There is catalog item called 'ABC' and I need to read all records from that table and that record are stored in one table. 

I need to call create service account and call API to retrieve all RITM and then we need to call API to retrieve table that stored User Registration data. 

Can you please help me with step. 

 

Regards, 

Nivedita 

 

 

 

1 ACCEPTED SOLUTION

@niveditakumari 

correct the scripted REST API resource needs to be given in postman

Also ensure you add prefix with the instance name etc in the URL

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

22 REPLIES 22

@niveditakumari 

share the script inside that scripted rest api

are you consuming the correct endpoint?

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

Hi @Ankur Bawiskar

 

Please find script inside scripted rest api : 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
    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 : 
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;
 
I have copied code which has been mentioned in link and created script include. 
I'm consuming endpoint for my uat instance : 
niveditakumari_0-1745239352512.png 

 

 

Postman : 
 
niveditakumari_1-1745239405175.png 

 

 
Do I need to use authentication and authorization for Postman: 
niveditakumari_2-1745239482941.png

 

Can you please confirm for above point and help me to correct that. 

 

Regards, 

Nivedit 

 

 

 

@niveditakumari 

you need to give endpoint of scripted rest api resource and not the scripted rest api

AnkurBawiskar_0-1745240245233.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar

 

Resource path - /api/kpm50/ritm_vars_api 

You are saying above scripted rest api resource endpoint need to add in Postman endpoint? 

Can you please help where exactly need to add that. 

 

Regards, 

Nivedita 

 

 

Hi @Ankur Bawiskar

 

I need to complete that by today evening. 

Resource path - /api/kpm50/ritm_vars_api 

Can you please confirm where exactly need to be add above Resource path. 

 

Regards, 

Nivedita