Bearer token for REST Integration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-11-2024 08:19 AM - edited ‎05-11-2024 08:20 AM
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'
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-13-2024 09:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-13-2024 10:22 PM
Thank you, and I received the assistance I needed with the new post.