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.
-
From the Resources menu, select Scripts.
-
At the right, click + Create script.
-
Name your new script (ex. exportVirtual_Script).
-
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 theNSObject
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.
-
Navigate to
→ . -
Click + Create API at the upper right.
-
Enter a name (ex. Demo – exports.runVirtual(options)).
-
Select the script saved in step A, exportVirtual_Script.
-
Click
to open the Script editor and copy the function name (
main_exportVirtual
). Paste the name into the Function field. -
Click Save & close.
-
Navigate to
→ . -
Click + Create API token at the upper right.
-
Enter a name (ex. Run virtual export).
-
In the Token permissions section, select Custom scopes.
-
From the JavaScript APIs list, select the JavaScript API you created in step B, (ex. Demo – exports.runVirtual(options)).
-
Click Save & close.
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} ] }
{ "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" ] }
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.