Skip to content

Commit 1ab8b64

Browse files
authored
Merge pull request #1684 from naveensnow/OutboundRESTDynamicEndpoint
Outbound Rest Dynamic endpoint
2 parents 22278fd + 9cbea2f commit 1ab8b64

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
This is a server-side Script Include that contains the core logic. It reads the endpoint configurations from a System Property, parses the JSON, and returns the appropriate URL based on the current instance's name.
2+
3+
System Property: x_my_scope.api.endpoints
4+
This property stores a JSON object containing the endpoint URLs for each environment. It must be created and populated in each instance that uses the utility.
5+
6+
Sample JSON object:
7+
{
8+
"dev": "https://dev-instance.example.com/api",
9+
"test": "https://test-instance.example.com/api",
10+
"prod": "https://prod-instance.example.com/api"
11+
}
12+
13+
Usage:
14+
var endpointConfig = new EndpointConfig();
15+
var endpointUrl = endpointConfig.getEndpoint();
16+
if (endpointUrl)
17+
{
18+
gs.info("Endpoint URL: " + endpointUrl);
19+
//Use the endpointUrl in your REST call
20+
var request = new sn_ws.RESTMessageV2();
21+
request.setEndpoint(endpointUrl);
22+
// ... rest of your integration logic
23+
} else
24+
{
25+
gs.error("Failed to retrieve endpoint URL.");
26+
}
27+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
//Create a sample system Property called x_my_scope.api.endpoints having below object as example. make sure your company instance includes those key such as dev,prod,test or modify it with your instance name
3+
4+
// {
5+
// "dev": "https://dev-instance.example.com/api",
6+
// "test": "https://test-instance.example.com/api",
7+
// "prod": "https://prod-instance.example.com/api"
8+
// }
9+
10+
var EndpointConfig = Class.create();
11+
EndpointConfig.prototype = {
12+
initialize: function() {
13+
// No hardcoded object here. It will be fetched from the System Property.
14+
},
15+
16+
getEndpoint: function() {
17+
var propertyName = 'x_my_scope.api.endpoints';
18+
var endpointObjectStr = gs.getProperty(propertyName);
19+
if (gs.nil(endpointObjectStr)) {
20+
gs.error("EndpointConfig: System property '" + propertyName + "' not found or is empty.");
21+
return null;
22+
}
23+
24+
try {
25+
var endpoints = JSON.parse(endpointObjectStr);
26+
var instanceName = gs.getProperty('instance_name');
27+
var environmentKey;
28+
29+
if (instanceName.includes('dev')) {
30+
environmentKey = 'dev';
31+
} else if (instanceName.includes('test') || instanceName.includes('uat')) {
32+
environmentKey = 'test';
33+
} else if (instanceName.includes('prod')) {
34+
environmentKey = 'prod';
35+
} else {
36+
gs.error("EndpointConfig: Could not determine environment for instance '" + instanceName + "'.");
37+
return null;
38+
}
39+
40+
if (endpoints.hasOwnProperty(environmentKey)) {
41+
return endpoints[environmentKey];
42+
} else {
43+
gs.error("EndpointConfig: Configuration not found for environment '" + environmentKey + "'.");
44+
return null;
45+
}
46+
47+
} catch (ex) {
48+
gs.error("EndpointConfig: Failed to parse JSON from system property '" + propertyName + "'. Exception: " + ex);
49+
return null;
50+
}
51+
},
52+
53+
type: 'EndpointConfig'
54+
};

0 commit comments

Comments
 (0)