Articles in this section

Run existing exports and imports with a JavaScript API

Use these examples to run an export or import that already exists in your account from a JavaScript API script. Each example shows the script and the request that calls it. They are for account owners and administrators who have already built a JavaScript API. To run an export, see the exports.run() script in Build a JavaScript API. To run a resource that exists only while the script runs, see Run virtual exports and imports with a JavaScript API.

For every example, Build a JavaScript API to create the script, the JavaScript API, and the API token, and then Test a JavaScript API. Each script's functions are described in integrator.io API and JavaScript runtime objects.

Run an import with supplied records

This example runs an existing NetSuite import and passes it the records that the calling application sends in the request body.

Create a script named JavaScript API – importExample with this code, replace YOUR_IMPORT_ID with the ID of your import, and enter _mainImportNS in the Function field of the JavaScript API:

import { imports } from 'integrator-api'

function _mainImportNS (options) {
  // Get the records passed in the request body
  let recordsToBeImported = options.body && options.body.records;

  if (!recordsToBeImported) {
    return {
      statusCode: 422,
      headers: { },
      body: { success: false, error: 'Provide the records to import in the request body.' }
    };
  }

  // Execute the import
  let importResponse;
  let statusCode = 200;
  try {
    importResponse = imports.run({ _id: 'YOUR_IMPORT_ID', data: recordsToBeImported });
  } catch (e) {
    importResponse = JSON.stringify(e);
    statusCode = 400;
  }

  return {
    statusCode: statusCode,
    headers: { },
    body: {
      method: options.method,
      queryString: options.queryString,
      importResponse: importResponse
    }
  };
}

The calling application sends the records in this format, with as many records in the array as it needs:

{
  "records": [
    {
      "id": "3097047072826",
      "first_name": "Demo",
      "last_name": "JavaScript API",
      "email": "Demo1@celigo.com"
    }
  ]
}

The field names must match the field names in the import mapping. After the call, check the destination application to confirm that the records synced.

Run multiple imports in sequence

This example runs two imports one after the other. The first import creates a customer in NetSuite. The second import creates a sales order for the customer ID that the first import returns.

Create a script named CreateCustomerAndSalesOrder_script with this code, replace YOUR_CUSTOMER_IMPORT_ID and YOUR_SALES_ORDER_IMPORT_ID with your import IDs, and enter handleRequest in the Function field of the JavaScript API:

import { imports } from 'integrator-api'

function handleRequest (options) {
  let response = {};
  let customerImportRes, salesOrderImportRes;

  // Check that the request provides data for the imports
  if (!options.body || !options.body.data) {
    response.statusCode = 422;
    response.body = { success: false, error: 'Please provide data' };
    return response;
  }

  try {
    let data = options.body.data;

    // Run the first import to create the customer using the data in the request body
    customerImportRes = imports.run({ _id: 'YOUR_CUSTOMER_IMPORT_ID', data: data.customer });

    // The response from the first import contains the new customer ID:
    // [{ "statusCode": 200, "id": 9999, "_json": { "id": 9999 } }]
    if (customerImportRes.length > 0) {
      // Run the second import to create the sales order for that customer
      salesOrderImportRes = imports.run({ _id: 'YOUR_SALES_ORDER_IMPORT_ID', data: customerImportRes });
    }
    response.statusCode = 200;
  } catch (err) {
    response.statusCode = 400;
    response.body = { success: false, error: err };
    return response;
  }

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

The request must include a data object that contains the customer data for the first import. If the request has no data object, the script returns the status code 422 and does not run either import.