Using RESTlet get, post, put, and delete entry points and methods

This sample is a RESTlet that shows how to use the get, post, put, and delete entry points and methods in a RESTlet script.

The conversion of this script from SuiteScript 1.0 to SuiteScript 2.1 includes the following:

SuiteScript 1.0 Script

SuiteScript 2.1 Script

                    // (GET method sample - get a standard NetSuite record)
function getRecord(datain) {
    return nlapiLoadRecord(datain.recordtype, datain.id);
} 

                  
                    // (POST method sample - create a standard NetSuite record)
function createRecord(datain) {
    var err = new Object();
   
    if (!datain.recordtype) {
        err.status = "failed";
        err.message= "missing recordtype";
        return err;
    }
   
    var record = nlapiCreateRecord(datain.recordtype);
   
    for (var fieldname in datain) {
       if (datain.hasOwnProperty(fieldname)) {
         if (fieldname != 'recordtype' && fieldname != 'id') {
             var value = datain[fieldname];
             if (value && typeof value != 'object') {
                 record.setFieldValue(fieldname, value);
             }
         }
       }
    }
    var recordId = nlapiSubmitRecord(record);
    return recordId;
} 

                  
                    // (DELETE method sample - delete a standard NetSuite record)
function deleteRecord(datain) {
    nlapiDeleteRecord(datain.recordtype, datain.id);
} 

                  
                    /**
 *@NApiVersion 2.1
 *@NScriptType Restlet
 */
define(['N/record', 'N/error'], (record, error) => {
    
    // Support method called by other methods for validating
    function doValidation(args, argNames, methodName) {
        for (let i = 0; i < args.length; i++) {
            if (!args[i] && args[i] !== 0) {
                throw error.create({
                    name: 'MISSING_REQ_ARG',
                    message: 'Missing a required argument: [' + argNames[i] + '] for method: ' + methodName
                });
             }
        }
    }
    
    // (GET method - get a standard NetSuite record)
    function _get(context) {
        doValidation([context.recordtype, context.id], ['recordtype', 'id'], 'GET');
        return JSON.stringify(record.load({
            type: context.recordtype,
            id: context.id
        }));
    }

    // (POST method - create a NetSuite record)
    function post(context) {
        doValidation([context.recordtype], ['recordtype'], 'POST');
        let rec = record.create({
            type: context.recordtype
        });
        for (var fldName in context)
            if (context.hasOwnProperty(fldName))
                if (fldName !== 'recordtype')
                    rec.setValue(fldName, context[fldName]);
        let recordId = rec.save();
        return String(recordId);
    }
             
    // (DELETE method - delete a standard NetSuite record)
    function _delete(context) {
        doValidation([context.recordtype, context.id], ['recordtype', 'id'], 'DELETE');
        record.delete({
            type: context.recordtype,
            id: context.id
        });
        return String(context.id);
    }
        
    // (PUT method - upsert a NetSuite record)
    function put(context) {
        doValidation([context.recordtype, context.id], ['recordtype', 'id'], 'PUT');
        let rec = record.load({
            type: context.recordtype,
            id: context.id
        });
        for (let 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
    };
}); 

                  

General Notices