Bearer token for REST Integration

tsoct
Tera Guru

I'm trying to get the list of organization codes via API, however using the script included below, I receive the error message 'GetCostCenterDetails:getORG:Error: undefined: no thrown error'.

What could have gone wrong?

var GetCostCenterDetails = Class.create();
GetCostCenterDetails.prototype = {
    initialize: function() {},

    getToken: function() {

        try {
            var rest = new sn_ws.RESTMessageV2('CostCenterCode', 'Get Token');
            rest.setLogLevel('all');    

            var response = rest.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();

            if (httpStatus.toString().startsWith("2")) {
                var parsedData = JSON.parse(responseBody);
                var token = parsedData.access_token;
				return token;
            } else {
                gs.error("Error occured ");
            }

        } catch (ex) {
               var message = ex.getMessage();
                gs.error("Token:Error: " + message);
        }
    },

    getORG: function() {

        try {
	    var result = {};
            var fund = 'GF'; //testing purpose
            var rest1 = new sn_ws.RESTMessageV2('CostCenterCode', "Get ORG");
            rest1.setQueryParameter('p_fund', fund);
            rest1.setRequestHeader('Authorization', 'Bearer ' + token);

            var response1 = rest1.execute();

            var responseBody1 = response1.getBody();
            var httpStatus1 = response1.getStatusCode();

            if (httpStatus1.toString().startsWith("2")) {
                    var parsedData1 = JSON.parse(responseBody1);

                if (parsedData1.value) {
                    fields = [];
                    var keys = Object.keys(parsedData1.value[0]);
                    for (var i = 0; i < keys.length; i++) {
                        var resultObject = {};
                        resultObject.name = keys[i];
                        resultObject.display_value = parsedData1.value[0][keys[i]];
                        fields.push(resultObject);
                    }

                    result.fields = [];
                    result.fields = fields;
                }
            } else {
                gs.error('Error occured 2');
            }

        } catch (ex) {
            var message = ex.getMessage();
            gs.error("GetCostCenterDetails:getORG:Error: " + message);
        }
        return result;
    },

    type: 'GetCostCenterDetails'
};

 

6 REPLIES 6

Integration work via script is pro-code, so will be technical. If you're unsure how to proceed then I'd recommend working with a ServiceNow partner to continue. Same answer for your new post: REST API and script include - ServiceNow Community.

 

Alternatively, look into using Integration Hub which is ServiceNow's low-code offering for integration work. NowLearning is a great resource on technical how-tos

 

Learn Integrations on the Now Platform (servicenow.com)

REST Integrations | ServiceNow Developers

Thank you, and I received the assistance I needed with the new post.