Articles in this section

Invoke a JavaScript API virtual export

This example creates a virtual export – a resource that exists only at runtime, not added to your integrator.io account – in JavaScript APIs and runs it to demonstrate the record returned from the source application.

A. Write the script

  1. From the Resources menu, select Scripts.

  2. At the right, click + Create script.

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

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

    • The script must have at least one entry-point function – main_exportVirtual() below.

    • It must handle any errors.

    • In this case, it calls exports.runVirtual(), passing it the export’s ID.

    • Define the export with the options object definition, as shown below in the NSObject JSON structure:

/*
* 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 { exports } from 'integrator-api'

function main_exportVirtual (options) {
  let NSObject;
  let invokeExportResponse;
  let response = {};
  
  NSObject = return_NS_Export_Obj (options);
  
  //Execute the export
  try {
    invokeExportResponse = exports.runVirtual({export:NSObject});
    response.statusCode = 200;
  } catch(e) {
    invokeExportResponse = JSON.stringify(e);
    response.statusCode = 400;
  }

  // Create body response
  response.body =  invokeExportResponse;
  
  return {
    statusCode: response.statusCode,
    headers: { },
    body: response.body
  }
}

function return_NS_Export_Obj (options) {
  let NSObject;
  let amount = options.body.records[0].amount;
  
  NSObject = {
    "_connectionId": "5e9de3074242783b222f56e0",
    "netsuite": {
      "type": "restlet",
      "skipGrouping": true,
      "statsOnly": false,
      "restlet": {
        "recordType": "invoice",
        "searchId": "6763",
        "criteria": [
          {
            "field": "amount",
            "operator": "greaterthan",
            "searchValue": <amount>, // Use param received from request
           }
        ]
      },
      "distributed": {
        "disabled": false,
        "forceReload": false,
        "executionContext": [
          "userinterface",
          "webstore"
        ],
        "executionType": [
          "create",
          "edit",
          "xedit"
        ]
      }
    },
    "adaptorType": "NetSuiteExport"
  }
  return NSObject;
}
  • Save the script.

B. Create the JavaScript API

  1. Navigate to ResourcesAPIJavaScript.

  2. Click + Create API at the upper right.

  3. Enter a name (ex. Demo – exports.runVirtual(options)).

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

  5. Click pencil.svg to open the Script editor and copy the function name ( main_exportVirtual ). Paste the name into the Function field.

  6. Click Save & close.

Create_myAPI.png

C. Get an API token

  1. Navigate to ResourcesAPI token.

  2. Click + Create API token at the upper right.

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

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

  5. From the JavaScript APIs list, select the JavaScript API you created in step B, (ex. Demo – exports.runVirtual(options)).

  6. Click Save & close.

D. Test the virtual export

Open Postman and connect to the JavaScript API created above at the provided URL, with the following values:

POST /v1/apis/5f3322bbcc1912681726f7d6/request HTTP/1.1
Host: api.staging.integrator.io
content-type: application/json
Authorization: Bearer d56••••••••••••••••••c06
Content-Type: text/plain

{
 "records":[
   {"amount":1000}
 ] 
}
360065874852-my-api-export-runvirtual-2.png

Sample response

{
  "data": [
    {
      "id": "313097",
      "recordType": "invoice",
      "Order Type": "",
      "*": " ",
      "Date": "8/14/2019",
      "As-Of Date": "8/14/2019",
      "Period": "Aug 2019",
      "Tax Period": "",
      "Type": "Invoice",
      "Document Number": "INV73910366",
      "Name": "Ariba Customer",
      "Account": "4000 Sales",
      "Memo": "",
      "Billing Account": "",
      "Amount": "2000.00",
      "CeligoAT_BigCommerceOrderID": "552",
      "Item": "Demo core product 2"
    }
  ],
  "dataURIs": [
    "https://tstdrv1934805.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=313097&compid=TSTDRV1934805"
  ]
}

E. Expose to API Management (optional)

You can expose this JavaScript API using Celigo's API Management so your API consumers can invoke it. You'll have full control over how the API is authenticated, documented, and managed. Learn to expose a JavaScript API.