Articles in this section

Trigger multiple imports through JavaScript APIs

This article assumes that you have created an integrator.io JavaScript API and given it permission within an API token. In this example, you’ll be triggering multiple imports at once using JavaScript API.

A. Write the script

  1. From the Resources menu, select Scripts.

  2. At the right, click + Create script.

  3. Name your new script (ex. CreateCustomerAndSalesOrder_script).

  4. Write the JavaScript code in the Edit content field.

    1. The script must have at least one entry-point function – handleRequest.

    2. It must handle any errors.

    3. In this case, it calls handleRequest (options), which runs the customer import and sales order import.

Invoke multiple imports script

The script contains two imports that will be invoked synchronously. The first import creates a customer record in NetSuite, after which the second import gets triggered, which will create a Sales order in NetSuite for the customer ID that was generated from the 1st import.

      /*
* handleRequest function stub:
*
* The name of the function can be changed to anything you like.
*
* The function will be passed one 'options' argument that has the following fields:
*   'method' - http request method (uppercase string).
*   'headers' - http request headers (object).  
*   'queryString' - http request query string (object).
*   'body' - parsed http request body (object, or undefined if unable to parse).
*   'rawBody' - raw http request body (string).
*
* The function needs to return a response object that has the following fields:
*   'statusCode' - http response status code (number).
*   'headers' - http response headers overrides (object, optional).
*   'body' - http response body (string or object).
* Throwing an exception will signal an error.
*/
import { imports } from 'integrator-api'

function handleRequest (options) {
  let data, customerImportRes, salesOrderImportRes
  let success = true
  let response = {}

  // Checking the request to run the script provides data for the imports
  if (!options.body.data) {
    response.body = { success: false, error: 'Please provide data' }
    response.statusCode = 422
    return response
  }

  try {
    // Running the first import to create customer in netsuite using the data supplied in the request body
    data = options.body.data
    customerImportRes = imports.run({ _id: '631645e4f8bf2d2b17f5fbd2', data: data.customer })
    // Response from the first import (customerImportRes)
    /* 
      [{
        "statusCode": 200,
        "id": 9999,
        "_json": {
            "id": 9999
        }
      }]
    */
    
    if (customerImportRes.length > 0) {
      // Running the second import
      salesOrderImportRes = imports.run({ _id: '6316ca125bca603be6bc2266', data: customerImportRes })
    }
    response.statusCode = 200
  } catch (err) {
    success = false
    response.statusCode = 400
    reponse.body = {success: false, error: err}
    return response
  }

  response.body = {
    success,
    customer: customerImportRes,
    salesOrder: salesOrderImportRes
  }

  return response
}

B. Create the JavaScript API

  1. Navigate to ResourcesAPIsJavaScript.

  2. Click + Create API at the upper right.

  3. Enter a name (ex. Demo – imports.runMultipleNetSuiteImports(options)).

  4. Select the script saved in step A, CreateCustomerAndSalesOrder_script.

  5. Click to open the Script editor and copy the function name (handleRequest).

  6. Paste the name into the Function field.

  7. Click Save & close.

C. Get an API token

  1. Navigate to ResourcesAPI tokens.

  2. Click + Create API at the upper right.

  3. Enter a name (ex. Run virtual imports).

  4. In the Token permissions section, select Custom scopes.

  5. From the JavaScript API(s) list, select the JavaScript API you created in step B, (ex. Demo – imports.runMultipleNetSuite(options)).

  6. Click Save & close.