Articles in this section

Trigger multiple imports through My API

This article assumes that you have created an integrator.io My API and given it permission within an API token. In this example, you’ll be triggering multiple imports at once using My 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.

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 My API

  1. Navigate to Resources → My APIs.
  2. Click + Create My 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 Resources → API tokens.
  2. Click + Create My 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 My APIs list, select the My API you created in step B, (ex. Demo – imports.runMultipleNetSuite(options)).
  6. Click Save & close.
Was this article helpful?
0 out of 0 found this helpful

Comments

1 comment
Date Votes
  • The multiple imports section actually came from a use case I had, and had been working with Celigo on implementing. I am not trying to use it myself, but running into a snag. 

    1. The .length property doesn't seem to work, and so I changed it to .id for the variable I was trying to reference and that works. 

    if (customerImportRes.id) {
          //data.data.customer = customerImportRes.id;
          try {
            customerImportRes.data = data.data;
            customerImportRes.data.customer = customerImportRes.id;
            console.info("data", JSON.stringify(customerImportRes));
            // Running the second import
          caseImportRes = imports.run({ _id: 'xxxxxxxxx', data: customerImportRes });
            console.info("caseImportRes", caseImportRes);
            customerImportRes.id = null;
          } catch (err) {
            console.log(err);
            
          }     
          
      }

    2. I had to add my id to the initial data load for NetSuite in order to create the case.

    3. However, the second import appears to run three times, dropping data each run, causing it to fail. It does create a case the first time, but then errors out because of the second and third runs.

    0

Please sign in to leave a comment.