Skip to content

Commit 3d8b145

Browse files
authored
UPS Tracking (#1993)
* Create UPS.js * Rename UPS.js to trackUPS.js * Create README.md * Update README.md
1 parent 9215b5e commit 3d8b145

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This script calls the UPS tracking API.
2+
3+
UPS Developer Account:
4+
Sign up at https://developer.ups.com
5+
Create an App to get credentials
6+
1. Client ID
7+
2. Client Secret
8+
9+
How to use:
10+
1. Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your UPS credentials.
11+
2. Use the sandbox URL (wwwcie.ups.com) for testing and production URL (onlinetools.ups.com) for live data.
12+
3. You can move this logic into a Script Include and call it from a Flow, Business Rule, or Catalog Client Script.
13+
4. For security, store credentials in a Connection & Credential Alias and reference them in the script instead of hardcoding.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(function executeUPSLookup() {
2+
try {
3+
var trackingNumber = '1Z12345E1512345676'; // replace or pass as a variable
4+
5+
// Step 1: Get OAuth token from UPS
6+
var tokenRequest = new sn_ws.RESTMessageV2();
7+
tokenRequest.setEndpoint('https://wwwcie.ups.com/security/v1/oauth/token');
8+
tokenRequest.setHttpMethod('POST');
9+
tokenRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
10+
tokenRequest.setBasicAuth('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
11+
tokenRequest.setRequestBody('grant_type=client_credentials');
12+
13+
var tokenResponse = tokenRequest.execute();
14+
var tokenBody = tokenResponse.getBody();
15+
var tokenObj = JSON.parse(tokenBody);
16+
var accessToken = tokenObj.access_token;
17+
18+
gs.info('UPS OAuth token retrieved successfully.');
19+
20+
// Step 2: Use token to request tracking info
21+
var trackingRequest = new sn_ws.RESTMessageV2();
22+
trackingRequest.setEndpoint('https://wwwcie.ups.com/api/track/v1/details/' + trackingNumber);
23+
trackingRequest.setHttpMethod('GET');
24+
trackingRequest.setRequestHeader('Authorization', 'Bearer ' + accessToken);
25+
trackingRequest.setRequestHeader('transId', gs.generateGUID());
26+
trackingRequest.setRequestHeader('transactionSrc', 'ServiceNow');
27+
28+
var trackingResponse = trackingRequest.execute();
29+
var trackingBody = trackingResponse.getBody();
30+
var trackingObj = JSON.parse(trackingBody);
31+
32+
gs.info('UPS Tracking Info: ' + JSON.stringify(trackingObj, null, 2));
33+
34+
// Example: log current status
35+
if (trackingObj.trackResponse && trackingObj.trackResponse.shipment) {
36+
var shipment = trackingObj.trackResponse.shipment[0];
37+
var status = shipment.package[0].activity[0].status.description;
38+
gs.info('Current Status: ' + status);
39+
}
40+
} catch (ex) {
41+
gs.error('Error pulling UPS tracking info: ' + ex.message);
42+
}
43+
})();

0 commit comments

Comments
 (0)