- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 08:32 AM
The script included below is returning an undefined error. How can I pass the bearer token to other function within the script include?
Imagine i will have more function within this script include, and not limited to only one getORG
var GetCostCenterDetails = Class.create();
GetCostCenterDetails.prototype = {
initialize: function() {},
_getToken: function() {
try {
var rest = new sn_ws.RESTMessageV2('CPNYCostCenter', 'Get Token');
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('No token generated');
}
} catch (ex) {
gs.error("\nException occured: " + ex);
}
},
getORG: function() {
var result = {};
try {
var token = this._getToken();
var rest1 = new sn_ws.RESTMessageV2('CPNYCostCenter', "Get ORG");
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 ORG');
}
} catch (ex) {
var message = ex.getMessage();
gs.error("GetCostCenterDetailsError: " + message);
}
return result;
},
type: 'GetCostCenterDetails'
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 03:25 PM
Try with
initialize: function() {
this.token = "" ? this._getToken() : this.token;
},
and later assign the value to it like
if (httpStatus.toString().startsWith("2")) {
var parsedData = JSON.parse(responseBody);
var token = parsedData.access_token;
return token;
and
try {
//var token = this._getToken();
var rest1 = new sn_ws.RESTMessageV2('CPNYCostCenter', "Get ORG");
rest1.setRequestHeader('Authorization', 'Bearer ' + this.token);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 09:54 AM
getACTIVITY2: function() {
try {
var r = new sn_ws.RESTMessageV2('CPNYCostCenter', 'Get ACTIVITY');
r.setStringParameterNoEscape('p_fund', 'AF');
r.setStringParameterNoEscape('p_org', 'CSIPM');
r.setRequestHeader('Authorization', 'Bearer ' + this.token);
var response2 = r.execute();
var responseBody2 = response2.getBody();
var httpStatus2 = response2.getStatusCode();
gs.info("The Activity: " + responseBody2);
return responseBody2;
} catch (ex) {
var message = ex.message;
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 03:43 PM
Also, if you haven’t initialized the script include itself, the this.token wont be initialized as well. So call your function with “mew GettCostCenterDetails().get ACTIVITY2();.
also - good to pass your params to this function adgetACTIVITY2(p_fund, p_org) .