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
- From the Resources menu, select Scripts.
- At the right, click + Create script.
- Name your new script (ex. CreateCustomerAndSalesOrder_script).
- Write the JavaScript code in the Edit content field.
- The script must have at least one entry-point function – handleRequest.
- It must handle any errors.
- 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
- Navigate to Resources → My APIs.
- Click + Create My API at the upper right.
- Enter a name (ex. Demo – imports.runMultipleNetSuiteImports(options)).
- Select the script saved in step A, CreateCustomerAndSalesOrder_script.
- Click to open the Script editor and copy the function name (handleRequest).
- Paste the name into the Function field.
- Click Save & close.
C. Get an API token
- Navigate to Resources → API tokens.
- Click + Create My API at the upper right.
- Enter a name (ex. Run virtual imports).
- In the Token permissions section, select Custom scopes.
- From the My APIs list, select the My API you created in step B, (ex. Demo – imports.runMultipleNetSuite(options)).
- Click Save & close.
Comments
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.
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.
Please sign in to leave a comment.