Articles in this section

Invoke a My API virtual export

This example creates a virtual export – a resource that exists only at runtime, not added to your integrator.io account – in My 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 My API

  1. Navigate to Resources >My APIs.

  2. Click + Create My 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.

360066080571-my-api-export-runvirtual.png

C. Get an API token

  1. Navigate to Resources > API 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 My APIs list, select the My 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 My 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 My 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 MyAPI.

Was this article helpful?
1 out of 1 found this helpful

Comments

4 comments
Date Votes
  • Hi Stephen,
    Would it be possible to clarify the NSObject in the Script? It is referencing also a restlet object which contains a search id reference. Does this mean that you are running a restelet within Netsuite or a Saved Seach. It would be great if you can mention any setup that is needed on the NetSuite side.
    I also created the script and it generates an error with the <amount>  statement.
    Any further clarification would be greatly appreciated as I am very interested to create a MyAPI to return a reponse from a Restlet in NetSuite or from an Export by passing a parameter.

    0
  • Jorge Iriarte for this to work in your environment, you need to change the _connectionId to your the connection id of your NetSuite connection within IO. To find this, you need to open that connection in Celigo, then look in the url and copy the id at the end. You also need to change the searchId to be your own search id which you can find in your NetSuite instance. As for <amount>, this is a field being sent to the myAPI in the request body. I think the <> are there just to call it out, but you should remove that.

    The easiest way to build the NSObject JSON is to make an export within the UI, then get the id of the export, then call our apis to GET that resource and just copy the full JSON. Here are the docs for getting export data: https://docs.celigo.com/hc/en-us/articles/7707693269275-Export-API-endpoints.

    0
  • let amount = options.body.records[0].amount;

    I am getting an error in this piece of code when i am trying to trigger it with POSTMAN, Here is the error:
    {
        "errors": [
            {
                "code": "script_error",
                "message": "TypeError: Cannot read property 'records' of undefined",
                "source": "JavaScript(api)",
                "stage": "invoke",
                "location": "Get sales Order Netsuite[API]:50",
                "stack": "TypeError: Cannot read property 'records' of undefined\n    at return_NS_Export_Obj (Get sales Order Netsuite[API]:50:29)\n    at main_exportVirtual (Get sales Order Netsuite[API]:27:14)"
            }
        ]
    }




    0
  • Aryan Srivastava are you sending the example post body?

    {
    "records":[
    {"amount":1000}
    ]
    }
    0

Please sign in to leave a comment.