Pre-req query script

  • Goal: Create a REST resource that can run a pre-req script like you can in postman

  • Steps: I have a JS script in an app that then triggers a query, but I would have to create a script for every type of request, however it requires CryptoJS, which I can't use in the resource setup

let rdate = moment().format("DDMMYYYYHHmmss");
let rnonce = (Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)).substr(0,20);
let hashstr = X_DICE_APPKEY.value + 
    X_DICE_USERNAME.value + 
    rdate + 
    rnonce + 
    X_DICE_PASSWORD.value + 
console.log('hashstr: ', hashstr)
let hashed = CryptoJS.SHA1(hashstr).toString();

return query4.trigger()
  • Screenshots:

Am I stuck with needing a script in the app for each query, or is there a better way I can do this?

Would it be possible to shunt the REST API request to a workflow where you can build the various pre-request scripting for the different types of requests you need to make? This way, in any app, you could call the workflow with the request type and have the workflow return the appropriate response data used by the app.

You would still need to replace the REST API query with the workflow resource in your apps but at least this way you can keep the logic all in one place instead of scattered across your app landscapes and keep DRY.

I ended up running the pre-req auth script on load, and then running it after any query that uses the auth, so its renewed for the next query. Seems a little dirty, but it works for now.