Javascript to pull tracking information from DHL website

  • 0
  • 1
  • Question
  • Updated 2 years ago
  • Answered
I am fairly new to quickbase and have never worked with javascript before, so simple answers based on this would be helpful.

In my app I have a table that shows all of my shipments and I would like to automatically have a field for each record that shows the current location, when it arrived there, and what the status is (e.g. at customs, delivered, etc.).

All of this information is available on DHL Express's tracking website, and is found in the table class "result-checkpoints show". I need to pull the date in the column header of the latest record and the record details to have all the information needed (no need to pull the older records for the shipment).

I currently have a button in my table that takes me to the tracking page based on the tracking # for the record but this is cumbersome to do for many records.

I know real-time/scheduled updates probably wouldn't be possible but I can create an update button afterwards to run the script and that would solve my problem. Does anybody know where I can find a Javascript formula such as this? Or can explain to me how to set up the GET function in a case such as this? A basic example would be helpful.
Photo of Alan Lundquist

Alan Lundquist

  • 122 Points 100 badge 2x thumb

Posted 2 years ago

  • 0
  • 1
Photo of Matthew Neil

Matthew Neil

  • 31,698 Points 20k badge 2x thumb
What you are looking for is a custom code page that takes advantage of the QB and DHL API.  Its not just a straight js page.  You could use a js page to force the update.  But if you have an integration between the system you could have it run every 15 min even without the need to push a button.
Photo of Harrison Hersch2

Harrison Hersch2

  • 400 Points 250 badge 2x thumb
A great solution to this is to use a solution like Aftership, combined with Zapier, to write updates to Quick Base when there is a tracking event.
Photo of Alan Lundquist

Alan Lundquist

  • 122 Points 100 badge 2x thumb
Thank you, I'll look into the Aftership and Zapier option as a fix for the moment as we are already utilizing Zapier for other integrations with Quickbase. Perhaps at a later date I'll pursue the full integration if we have to expand or need further customization.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
Apparently DHL has an API that supports JavaScript and authenticates using an OAuth 2.0 Bearer Token. I was able to us their sample code and test credentials to receive an access token. I didn't go further as I don't have a DHL account but this appears to be straightforward.

const serverUrl = "https://api-qa.dhlecommerce.com/v1";
function getAuthToken(clientId, clientSecret, callback) {
    var endpoint = "/auth/accesstoken";
    // Creating new instance of XMLHttpRequest
    var xhr = new XMLHttpRequest();
    // Configure request
    xhr.open('GET', serverUrl + endpoint, true); // true for asynchronous request
    // Encoding credentials in base-64 form
    var authString = "Basic " + btoa(clientId + ":" + clientSecret);
    // Setting HTTP headers
    xhr.setRequestHeader('Accept', 'application/json');
    xhr.setRequestHeader('Authorization', authString);
    // Send request to sever
    xhr.send();
    xhr.onreadystatechange = function() {
      // If request is not complete - return
      if (this.readyState != 4) return;
      // If response status code is not OK
      if (this.status != 200) {
        console.log("Error: " + (this.status ? this.statusText : "reqeust error"));
        return;
      }
      // Call our callback function to pass the result of the request
      callback(this.responseText);
    };
}

var clientId = "e9ed82a8-4237-4185-8e36-47264aa9e718";
var clientSecret= "b1ed1bfa-689b-4d5b-bbf2-39dde64ccb64";
callback = function(result) {
  console.log(JSON.stringify(result, null, "  "));
}
//output:
{
  "access_token": "Cf5QrrLDJ6cnHEcD9m06j3HNOf22Lbjvjr58kKKb6Ow31di9Z0M8nE",
  "token_type": "Bearer",
  "expires_in": 18000,
  "scope": "shipping efulfillment label"
}

See: https://pwa.tips/2017/08/24/Issue-7.html
(Edited)