Skip to content

Commit ce8ea14

Browse files
authored
Merge pull request #2202 from LakshmiChaitanyaReddy/Chaitanya-ILCR-Code-Snippets-v6
Chaitanya ilcr code snippets v6
2 parents 02ea335 + 50fe773 commit ce8ea14

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
RestMessageUtils
2+
A utility Script Include for simplifying REST API calls in ServiceNow using sn_ws.RESTMessageV2.
3+
Features
4+
5+
Supports dynamic or named REST messages.
6+
Easily set headers, query parameters, and request body.
7+
Supports Basic Auth, Auth Profiles, and API Key authentication.
8+
Optional MID Server configuration.
9+
Handles variable substitution.
10+
Centralized error handling with gs.error() logging.
11+
Designed for use in Script Includes, Business Rules, or Scripted REST APIs.
12+
Lightweight and reusable for multiple integrations.
13+
14+
Object Structure
15+
/*
16+
ObjectStructure = {
17+
endPoint: 'enpoint',
18+
httpMethod: 'Add Method',
19+
queryParams: {
20+
key1: 'value',
21+
key2: 'value'
22+
},
23+
requestHeaders: {
24+
},
25+
authType: 'CHOICES among [BasicCreds,BasicAuthProfile,APIKEY]',
26+
authProfile: 'sysid of auth profile if authtype is selected as BasicAuthProfile',
27+
userName: 'userName',
28+
pasword: 'password',
29+
midServer: 'midServer',
30+
31+
}
32+
*/
33+
34+
Example Usage
35+
/*
36+
var obj = {
37+
endPoint: 'https://instancename.service-now.com/api/now/table/problem',
38+
queryParams: {
39+
sysparm_query: 'active=true',
40+
sysparm_limit: 2,
41+
sysparm_fields: 'number,short_description'
42+
},
43+
httpMethod: 'POST',
44+
authType: 'BasicCreds',
45+
userName: 'admin',
46+
password: gs.getProperty('dev204127.admin.password'),
47+
requestHeaders: {
48+
Accept: 'Application/JSON'
49+
},
50+
51+
52+
};
53+
var resp = new RestMessageUtils(obj, 'Test RestMessage Utils', 'Default GET').execute();
54+
55+
gs.print(resp.getBody())
56+
*/
57+
58+
59+
60+
61+
62+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var RestMessageUtils = Class.create();
2+
RestMessageUtils.prototype = {
3+
initialize: function(rmObj, restMessage, restFunc) {
4+
this.RM = (restFunc && restMessage) ? new sn_ws.RESTMessageV2(restMessage, restFunc) : new sn_ws.RESTMessageV2();
5+
this.rmObj = rmObj;
6+
},
7+
8+
checkAndGetKeyValue: function(obj, key) {
9+
return obj.hasOwnProperty(key);
10+
11+
},
12+
13+
setQueryParams: function() {
14+
if (this.checkAndGetKeyValue(this.rmObj, 'queryParams'))
15+
for (var i in this.rmObj.queryParams)
16+
this.RM.setQueryParameter(i, this.rmObj.queryParams[i]);
17+
18+
},
19+
20+
variableSubs: function() {
21+
if (this.checkAndGetKeyValue(this.rmObj, 'variableInfo'))
22+
for (var i in this.rmObj.variableInfo)
23+
this.RM.setStringParameterNoEscape(i, this.rmObj.variableInfo[i]);
24+
},
25+
26+
setAuth: function() {
27+
var authType = this.rmObj.authType;
28+
29+
if (authType) {
30+
if (authType == 'APIKEY')
31+
return;
32+
else if (authType == 'BasicCreds')
33+
this.RM.setBasicAuth(this.rmObj.userName, this.rmObj.password);
34+
else if (authType == 'BasicAuthProfile')
35+
this.RM.setAuthenticationProfile('basic', this.rmObj.authProfile);
36+
37+
}
38+
39+
},
40+
41+
setRequestBody: function() {
42+
if (this.checkAndGetKeyValue(this.rmObj, 'requestBody'))
43+
this.RM.setRequestBody(typeof this.rmObj.requestBody == 'object' ? JSON.stringify(this.rmObj.requestBody) : this.rmObj.requestBody);
44+
},
45+
46+
setRequestHeaders: function() {
47+
if (this.checkAndGetKeyValue(this.rmObj, 'requestHeaders'))
48+
for (var i in this.rmObj.requestHeaders)
49+
this.RM.setRequestHeader(i, this.rmObj.requestHeaders[i]);
50+
},
51+
52+
setMidServer: function() {
53+
if (this.checkAndGetKeyValue(this.rmObj, 'midServer'))
54+
this.RM.setMidServer(this.rmObj.midServer);
55+
},
56+
57+
setEndpoint: function() {
58+
if (this.checkAndGetKeyValue(this.rmObj, 'endPoint'))
59+
this.RM.setEndpoint(this.rmObj.endPoint);
60+
},
61+
setHttpMethod: function() {
62+
if (this.checkAndGetKeyValue(this.rmObj, 'httpMethod'))
63+
this.RM.setHttpMethod(this.rmObj.httpMethod);
64+
},
65+
66+
execute: function() {
67+
try {
68+
this.setHttpMethod();
69+
this.setEndpoint();
70+
this.setRequestHeaders();
71+
this.setAuth();
72+
this.setRequestBody();
73+
this.setQueryParams();
74+
this.variableSubs();
75+
this.setMidServer();
76+
return this.RM.execute();
77+
} catch (err) {
78+
gs.error('REST Message execution failed: ' + err);
79+
}
80+
81+
},
82+
83+
84+
type: 'RestMessageUtils'
85+
};

0 commit comments

Comments
 (0)