Example of a RESTlet that Retrieves, Deletes, Creates, and Upserts a NetSuite Record
The following RESTlet uses each entry point to retrieve (get entry point), delete (delete entry point), create (post entry point), and upsert (put entry point) 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 retrieve a record by using this RESTlet, you would use the get method. To identify the record you want to retrieve, you would add values to the URL you use to call the RESTlet to identify the record type and internal ID of the record instance you want. These parameters are defined in the RESTlet’s get function as:
-
context.recordtype
-
context.id
You add a value for each parameter by using an ampersand, the name of the parameter, an equals sign, and the parameter’s value, as follows:
&[name of parameter]=[value]
For example, to retrieve a phone call record with the internal ID of 9363, you would the following URL (with script and deploy values set appropriately):
https://<accountID>.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=474&deploy=1&recordtype=phonecall&id=9363
Using the get method in conjunction with this URL would produce the following request:
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, the system would return data like the following:
{
"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 use this RESTlet to add a record, you would use the post method. Your arguments must be included in a request body, and the request body would have to be written in JSON rather than plain text. For example:
{"recordtype":"phonecall","type":"phonecall","title":"Project Kickoff"}
You would send your post method to a URL that has not been extended. For testing purposes, you could use the value that appears in the External URL field of the script deployment record. (However, in an integration, remember that you must dynamically discover the RESTlet domain. See Dynamically Generating a Full URL).
For example, you could call this RESTlet by using a URL like the following — one that does not include embedded parameters:
https://<accountID>.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=474&deploy=1
Making a post call using the request body above, plus the appropriate headers, would produce the following request:
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: ...
In response, the system returns the internal ID of the newly created record. For example:
9564
Related Topics
- SuiteScript 2.x RESTlet Script and Request Examples
- Example Hello World RESTlet
- Example of a RESTlet that Adds Multiple Records
- Example of a RESTlet that Manipulates Scheduled Script
- Example of a Client Script that Calls a RESTlet
- Example of a Suitelet that Calls a RESTlet
- Example of a Shell Script that Calls a RESTlet