Utopia Knowledge Base Definitions
  • Welcome to Utopia University
  • 🔦Overview
    • How to Navigate the University
    • How is Utopia different
    • Features
    • Utopia Sync Website Links
  • 😁Contact Us
    • Open Chat Room
    • Contact Us
  • ⬇️Installation guide
    • Docker Compose Installation
      • Prerequisites
      • The Installation Configurator
        • Databases
        • S3 (Optional)
        • SMTP Details (Compulsory)
        • Adobe API Credentials (Optional)
        • Final steps to get your compose fle
      • Compose Installation
    • Kubernetes Installation
      • Prerequisites
      • The Installation Configurator
        • Databases
        • S3 (Optional)
        • SMTP Details (Compulsory)
        • Adobe API Credentials (Optional)
        • Ingress Naming (Compulsory)
        • Final steps to get helm chart
      • Using Helm to Install and Edit
      • Final Stage DNS setup
        • Setting up TLS cert
        • Third Party Service DNS Manager (like Cloudflare)
        • DNS Work-around while you get your cert ready
    • Cluster Craft
      • Basic Kubernetes install with microk8s
      • Basic Kubernetes install with microk8s and NFS storage
      • Updating Loadbalancer IP with Microk8s and Metallb
      • Installing Utopia in Azure
      • Testing SMTP server from a Kubernetes Cluster or a Container using telnet and openssl
      • Kubernetes Visibility
        • Kube CLI visibility with Lens or OpenLens
        • Prometheus and Grafana Monitoring
      • Syntax Cheat Sheet
        • Kubectl
        • Microk8s
        • Microceph
        • Mongodb Compass
          • _MONGOSH Terminal Commands
          • Query Commands
        • Shell Commands
  • 🖥️Product Guide
    • Integration Hub
      • Selecting your Organization
      • Menu Navigation
      • Data In
        • Collection Parameters
        • Document Lifespan Strategy
        • Source Connectors
          • Connector Modules
            • Empact Email Orders
            • File Transfer Protocol (FTP)
            • Generate Random Documents (Test)
            • HTTP REST Collection Source
            • Lotus 1-2-3 prn file
            • Microsoft Navision Collection
            • MySQL Query
            • Pick n Pay Supplier Orders
            • Post Office Protocol (POP3)
            • Secure File Transfer Protocol (SFTP) (SSH)
            • Internet Message Access Protocol (IMAP)
            • Shoprite Collection
            • SQL Server Query
            • StrategixNav Collection
            • Transaction Query
          • Web Hook Triggers
        • API Listeners
          • Document Capture
          • Document Query (External)
          • Document Query (Integrated)
          • Listener Security
      • Transform
        • Search Documents
        • The Designer Panel
        • The Designer view
        • Transform Stage Types
          • Filter
          • Lookup
          • Add Field
          • Projection
          • Unwind
          • JS / JSON
      • Data Out
        • Publisher Collection Parameters
        • Data Out Connector
          • API Collection Publisher
          • Do Nothing
          • File Transfer Protocol (FTP)
          • HTTP REST Collection Publisher
          • Message Transport Post
          • Microsoft Navision Publisher
          • PDF Document Emailer
          • Pick n Pay Invoices
          • Post Syspro QueryQuery
          • Post Syspro Transaction
          • Post Vector Orders
          • Secure File Transform Protocol (SFTP) (SSH)
          • Shoprite Invoice Publisher
          • SMTP Collection Publisher
          • SQL Server OPENJSON Publisher
          • SQL Server Query Publisher
          • StrategixNav Publisher
      • Validation
    • Organizations
    • Transaction Page
    • User Profile
    • Logging
    • Process Que Dashboard
    • Process Error Dashboard
    • New Functions
      • XML to Json using the Transform Designer
      • XML to Json using ChatGPT
      • Rest Connector JS Prescript
    • Release Notes
      • Microservices
        • 0.12.1622
        • 0.12.1630
  • ☕Use Cases
    • Follow Along Examples
      • Look-ups! no-code Transform Stage
        • Multi Field Look-ups! low-code Transform Stage
      • Rest Connector JS Prescript Example
        • Rest Connector JS PreScript Fault Finding Techniques
      • XML to Json using the Transform Designer
      • XML to Json using ChatGPT
    • Solution Discussions
      • Moditar: Increasing Integration Adoption using UtopiaSync
      • UtopiaSync the scalable no-code Backend?
      • UtopiaSync Security - How does it work?
      • UtopiaSync Custom Configuration
      • UtopiaSync Maintenance
  • 😁Case Studies
    • How Moditar drove efficiency for Meridian Wine Merchants using Utopia iPaaS
    • The journey to Spier Wine Farm's perfect eCommerce Integration
    • Integration journey to 100% automated with La Concorde Bakery
Powered by GitBook
On this page
  1. Use Cases
  2. Follow Along Examples

Rest Connector JS Prescript Example

PreviousMulti Field Look-ups! low-code Transform StageNextRest Connector JS PreScript Fault Finding Techniques

Last updated 1 year ago

The Rest connector sometimes requires one to manipulate the data between calls, an example of this might be where a call will give you a batch size and total documents field.

For this example we are going to use the post-man echo functionality to retrieve a doc with the following fields:

{
    "args": {
        "batch": "50",
        "total": "215",
        "docNo": "1",
        "pageNo": "1"
    }
}    

We will use the JS script to cycle through the DocNum and Page number until you get to the end. Then start again from the beginning as seen in the above.

I am no JS expert so I am going to ask ChatGPT to build the code for me. see transcript here.

The output given needs a bit of cleaning up. they referenced the fields with fields instead of this... and gave us a bit more than we need. Utopia will wrap the script up correctly as a function so we only need to input the actual scripts. The return syntax can also be adjusted as below. So this is a good starting point:

    let batch = parseInt(args.batch, 10);
    let total = parseInt(args.total, 10);
    let docNo = parseInt(args.docNum, 10);
    let pageNo = parseInt(this.pageNo, 10);

    // Perform the specified calculation
    let newDocNo = docNo + batch;

    if (newDocNo > total) {
        // Reset docNo and pageNo if newDocNo is greater than total
        docNo = 1;
        pageNo = 1;
    } else {
        // Update docNo to newDocNo and increment pageNo
        docNo = newDocNo;
        pageNo += 1;
    }

    // Return the updated docNo and pageNo in the specified format
    return {'docNo': docNo, 'pageNo': pageNo};

we can now remove the spaces and comments and put it in a form we can use in the connector

let batch = parseInt(args.batch, 10); let total = parseInt(args.total, 10); let docNo = parseInt(args.docNum, 10); let pageNo = parseInt(this.pageNo, 10); let newDocNo = docNo + batch; if (newDocNo > total) { docNo = 1; pageNo = 1;} else { docNo = newDocNo; pageNo += 1;} return {'docNo': docNo, 'pageNo': pageNo};

No we have something we can use in the call. We must reference the parameters pageNo and docNo we are returning to use in the next call. In these examples the pre-request is going to run the call previous request call, with that result im memory the PreRequestScript can then run and update values for the main Request. So lets setup the Rest connector for our Data-In:

{
  "PreRequests": [
    {
      "Url": "https://postman-echo.com/get?batch=50&total=${total}&docNo=${docNo}&pageNo=${pageNo}",
      "Method": "GET"
    }
  ],
  "Request": {
    "Url": "https://postman-echo.com/get?batch=50&total=${total}&docNo=${docNo}&pageNo=${pageNo}",
    "Method": "GET",
    "PreRequestScript": {
      "Code": "let batch = parseInt(args.batch, 10); let total = parseInt(args.total, 10); let docNo = parseInt(args.docNo, 10); let pageNo = parseInt(this.pageNo, 10); let newDocNo = docNo + batch; if (newDocNo > total) { docNo = 1; pageNo = 0;} else { docNo = newDocNo; pageNo += 1;} return {docNo, pageNo};",
      "Language": "javascript"
    }
  },
  "Parameters": {
    "total": {
      "Path": "args.total",
      "InitialValue": "215"
    },
    "docNo": {
      "Path": "args.docNo",
      "InitialValue": "1"
    },
    "pageNo": {
      "Path": "args.pageNo",
      "InitialValue": "0"
    }
  }
}

Lets Create a data-in with this code using the

And here is the output, you can see the docNo and pageNo iterating up by the js script. This is the view of the documents in MongoDB directly using a tool MongoDB Compass:

{
  "PreRequests": [
    {
      "Url": "https://postman-echo.com/get?calculated=${calculated}",
      "Method": "GET",
      "PreRequestScript": {
        "Code": "return { 'calculated' : (this.random || 0) + this.constant }",
        "Language": "javascript"
      }
    }
  ],
  "Request": {
    "Url": "https://postman-echo.com/get?calculated=${calculated}",
    "Method": "GET"
  },
  "Parameters": {
    "calc": {
      "Path": "args.calculated"
    },
    "random": {
      "InitialValue": 5
    },
    "constant": {
      "InitialValue": 10
    }
  }
}

Here we have successfully implemented a js script in our setup. you can mix and match these where you need it. here is another example you can use in a data-in rest connector, this one uses the script in the pre-request. The PreRequestScript node is also available in the .

I hope this helps! let us know if you would like any specific examples. Have a look at the next page for suggestions.

☕
REST Collection Publisher
fault finding
2KB
ChatGPT_JS_Transcript.txt
Iterating Up
Iterating through reset back to 1 and 0