From 848823014f2035111e6a003b1064888d5994b650 Mon Sep 17 00:00:00 2001 From: Chaitanya Reddy <93564567+LakshmiChaitanyaReddy@users.noreply.github.com> Date: Fri, 17 Oct 2025 07:13:07 +0530 Subject: [PATCH 1/4] Create RestMessageUtils.js --- .../RestMessageUtils/RestMessageUtils.js | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Server-Side Components/Script Includes/RestMessageUtils/RestMessageUtils.js diff --git a/Server-Side Components/Script Includes/RestMessageUtils/RestMessageUtils.js b/Server-Side Components/Script Includes/RestMessageUtils/RestMessageUtils.js new file mode 100644 index 0000000000..18d80fb504 --- /dev/null +++ b/Server-Side Components/Script Includes/RestMessageUtils/RestMessageUtils.js @@ -0,0 +1,85 @@ +var RestMessageUtils = Class.create(); +RestMessageUtils.prototype = { + initialize: function(rmObj, restMessage, restFunc) { + this.RM = (restFunc && restMessage) ? new sn_ws.RESTMessageV2(restMessage, restFunc) : new sn_ws.RESTMessageV2(); + this.rmObj = rmObj; + }, + + checkAndGetKeyValue: function(obj, key) { + return obj.hasOwnProperty(key); + + }, + + setQueryParams: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'queryParams')) + for (var i in this.rmObj.queryParams) + this.RM.setQueryParameter(i, this.rmObj.queryParams[i]); + + }, + + variableSubs: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'variableInfo')) + for (var i in this.rmObj.variableInfo) + this.RM.setStringParameterNoEscape(i, this.rmObj.variableInfo[i]); + }, + + setAuth: function() { + var authType = this.rmObj.authType; + + if (authType) { + if (authType == 'APIKEY') + return; + else if (authType == 'BasicCreds') + this.RM.setBasicAuth(this.rmObj.userName, this.rmObj.password); + else if (authType == 'BasicAuthProfile') + this.RM.setAuthenticationProfile('basic', this.rmObj.authProfile); + + } + + }, + + setRequestBody: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'requestBody')) + this.RM.setRequestBody(typeof this.rmObj.requestBody == 'object' ? JSON.stringify(this.rmObj.requestBody) : this.rmObj.requestBody); + }, + + setRequestHeaders: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'requestHeaders')) + for (var i in this.rmObj.requestHeaders) + this.RM.setRequestHeader(i, this.rmObj.requestHeaders[i]); + }, + + setMidServer: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'midServer')) + this.RM.setMidServer(this.rmObj.midServer); + }, + + setEndpoint: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'endPoint')) + this.RM.setEndpoint(this.rmObj.endPoint); + }, + setHttpMethod: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'httpMethod')) + this.RM.setHttpMethod(this.rmObj.httpMethod); + }, + + execute: function() { + try { + this.setHttpMethod(); + this.setEndpoint(); + this.setRequestHeaders(); + this.setAuth(); + this.setRequestBody(); + this.setQueryParams(); + this.variableSubs(); + this.setMidServer(); + return this.RM.execute(); + } catch (err) { + gs.error('REST Message execution failed: ' + err); + } + + }, + + + type: 'RestMessageUtils' +}; From 6ac8da99fb482dab8b3765ff465ed197d70e0099 Mon Sep 17 00:00:00 2001 From: Chaitanya Reddy <93564567+LakshmiChaitanyaReddy@users.noreply.github.com> Date: Fri, 17 Oct 2025 07:16:35 +0530 Subject: [PATCH 2/4] Create README.md --- .../RestMessageUtils/README.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Server-Side Components/Script Includes/RestMessageUtils/README.md diff --git a/Server-Side Components/Script Includes/RestMessageUtils/README.md b/Server-Side Components/Script Includes/RestMessageUtils/README.md new file mode 100644 index 0000000000..261240f92f --- /dev/null +++ b/Server-Side Components/Script Includes/RestMessageUtils/README.md @@ -0,0 +1,63 @@ +RestMessageUtils +A utility Script Include for simplifying REST API calls in ServiceNow using sn_ws.RESTMessageV2. +Features + +Supports dynamic or named REST messages. +Easily set headers, query parameters, and request body. +Supports Basic Auth, Auth Profiles, and API Key authentication. +Optional MID Server configuration. +Handles variable substitution. +Centralized error handling with gs.error() logging. +Designed for use in Script Includes, Business Rules, or Scripted REST APIs. +Lightweight and reusable for multiple integrations. + +Object Structure +/* +ObjectStructure = { + endPoint: 'enpoint', + httpMethod: 'Add Method', + queryParams: { + key1: 'value', + key2: 'value' + }, + requestHeaders: { + + }, + authType: 'CHOICES among [BasicCreds,BasicAuthProfile,APIKEY]', + authProfile: 'sysid of auth profile if authtype is selected as BasicAuthProfile', + userName: 'userName', + pasword: 'password', + midServer: 'midServer', + +} +*/ + +Example Usage +/* +var obj = { + endPoint: 'https://dev204127.service-now.com/api/now/table/problem', + queryParams: { + sysparm_query: 'active=true', + sysparm_limit: 2, + sysparm_fields: 'number,short_description' + }, + httpMethod: 'POST', + authType: 'BasicCreds', + userName: 'admin', + password: gs.getProperty('dev204127.admin.password'), + requestHeaders: { + Accept: 'Application/JSON' + }, + + +}; +var resp = new RestMessageUtils(obj, 'Test RestMessage Utils', 'Default GET').execute(); + +gs.print(resp.getBody()) +*/ + + + + + + From 58ffff18df4310ae728bf4446cb4b1c00e96e23f Mon Sep 17 00:00:00 2001 From: Chaitanya Reddy <93564567+LakshmiChaitanyaReddy@users.noreply.github.com> Date: Fri, 17 Oct 2025 07:17:11 +0530 Subject: [PATCH 3/4] Update README.md --- .../Script Includes/RestMessageUtils/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/Server-Side Components/Script Includes/RestMessageUtils/README.md b/Server-Side Components/Script Includes/RestMessageUtils/README.md index 261240f92f..9028f008a0 100644 --- a/Server-Side Components/Script Includes/RestMessageUtils/README.md +++ b/Server-Side Components/Script Includes/RestMessageUtils/README.md @@ -21,7 +21,6 @@ ObjectStructure = { key2: 'value' }, requestHeaders: { - }, authType: 'CHOICES among [BasicCreds,BasicAuthProfile,APIKEY]', authProfile: 'sysid of auth profile if authtype is selected as BasicAuthProfile', From 50fe773c6a9093d4fa4f3975ff09a65f388215c8 Mon Sep 17 00:00:00 2001 From: Chaitanya Reddy <93564567+LakshmiChaitanyaReddy@users.noreply.github.com> Date: Fri, 17 Oct 2025 07:17:43 +0530 Subject: [PATCH 4/4] Update README.md --- .../Script Includes/RestMessageUtils/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server-Side Components/Script Includes/RestMessageUtils/README.md b/Server-Side Components/Script Includes/RestMessageUtils/README.md index 9028f008a0..4cd53d4ce3 100644 --- a/Server-Side Components/Script Includes/RestMessageUtils/README.md +++ b/Server-Side Components/Script Includes/RestMessageUtils/README.md @@ -34,7 +34,7 @@ ObjectStructure = { Example Usage /* var obj = { - endPoint: 'https://dev204127.service-now.com/api/now/table/problem', + endPoint: 'https://instancename.service-now.com/api/now/table/problem', queryParams: { sysparm_query: 'active=true', sysparm_limit: 2,