Example of a RESTlet that Retrieves, Deletes, Creates, and Upserts a NetSuite Record

This RESTlet uses each entry point to retrieve (get), delete, create (post), and upsert (put) NetSuite records.

RESTlet

            /**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */
define(['N/record', 'N/error'],
    function(record, error) {
        // Get a standard NetSuite record
        function _get(context) {
            return JSON.stringify(record.load({
                type: context.recordtype,
                id: context.id
            }));
        }
        // Delete a standard NetSuite record
        function _delete(context) {
            record.delete({
                type: context.recordtype,
                id: context.id
            });
            return String(context.id);
        }
        // Create a NetSuite record from request params
        function post(context) {
            var rec = record.create({
                type: context.recordtype
            });
            for (var fldName in context) {
                if (context.hasOwnProperty(fldName))
                    if (fldName !== 'recordtype')
                        rec.setValue(fldName, context[fldName]);
            }
            var recordId = rec.save();
            return String(recordId);
        }
        // Upsert a NetSuite record from request param
        function put(context) {
           var rec = record.load({
                type: context.recordtype,
                id: context.id
            });
            for (var fldName in context) {
                if (context.hasOwnProperty(fldName))
                    if (fldName !== 'recordtype' && fldName !== 'id')
                        rec.setValue(fldName, context[fldName]);
            }
            rec.save();
            return JSON.stringify(rec);
        }
        return {
            get: _get,
            delete: _delete,
            post: post,
            put: put
        };
    }); 

          

Example Get Call and Response

To get a record with this RESTlet, use the get method. In the URL, add values for record type and the internal ID of the record you want. The RESTlet’s get function uses these parameters:

  • context.recordtype

  • context.id

Add each parameter to the URL using an ampersand, the parameter name, an equals sign, and its value:

            &[name of parameter]=[value] 

          

For example, to get a phone call record with internal ID 9363, use a URL like this (make sure the script and deploy values are correct):

            https://<accountID>.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=474&deploy=1&recordtype=phonecall&id=9363 

          

Using the get method with this URL, your request would look like this:

            GET /app/site/hosting/restlet.nl?script=474&deploy=1&recordtype=phonecall&id=9363 HTTP/1.1
HOST: <accountID>.restlets.api.netsuite.com  
authorization: NLAuth nlauth_account=12345, nlauth_email=john@smith.com, nlauth_signature=Welcome123  
content-type: application/json  
cookie: ... 

          

In response, you’ll get data like this:

            {
 "id": "9363"
 "type": "phonecall"
 "isDynamic": false
 "fields": {
  "wfinstances": ""
  "nlloc": "0"
  "nlsub": "1"
  "createddate": "5/18/2016 1:01 am"
  "timezone": "America/Los_Angeles"
  "accesslevel": "F"
  "_eml_nkey_": "143659438"
  "starttime": ""
  "startdate": "5/18/2016"
  "title": "Project kickoff"
  "type": "call"
  ...
 } 

  "sublists": {
    "contact": {
     "currentline": {
      "company": ""
      "contact": ""
      ...
   }-
  }-
 }-
} 

          

Example Post Call and Response

To add a record with this RESTlet, use the post method. Arguments go in the request body, and the body needs to be in JSON, not plain text. For example:

            {"recordtype":"phonecall","type":"phonecall","title":"Project Kickoff"} 

          

Send your post request to a URL that doesn’t include extra parameters. For testing, use the value in the External URL field on the deployment record. For integrations, make sure you dynamically get the RESTlet domain. See Dynamically Generating a Full URL).

For example, you can call this RESTlet with a URL like this (no parameters):

            https://<accountID>.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=474&deploy=1 

          

If you make a post call with that request body and the right headers, your request would look like this:

            POST /app/site/hosting/restlet.nl?script=474&deploy=1 HTTP/1.1  
HOST: <accountID>.restlets.api.netsuite.com  
authorization: NLAuth nlauth_account=12345, nlauth_email=john@smith.com, nlauth_signature=Welcome123  
content-type: application/json  
cookie: ... 

          

The RESTlet then returns the internal ID of the new record. For example:

            9564 

          

Related Topics

General Notices