RESTLett script with sample example

RESTlets, written in SuiteScript, serve as intermediaries between NetSuite and external systems, adhering to the principles of Representational State Transfer (REST) and utilizing standard HTTP methods (GET, POST, PUT, DELETE) for resource operations. 

Example: Creating a Customer via RESTlet

Let’s consider a scenario where an external system needs to create a new customer record in NetSuite. We’ll create a RESTlet script to handle this operation.

/**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 */
define(['N/record'],
    (record) => {
        const post = (requestBody) => {
            let newCustomer = record.create({
                type: record.Type.CUSTOMER,
                isDynamic: true
            });


            // Set customer field values based on request data
            newCustomer.setValue({
                fieldId: 'companyname',
                value: requestBody.companyName
            });
            newCustomer.setValue({
                fieldId: 'email',
                value: requestBody.email
            });


            // Save the customer record
            let customerId = newCustomer.save();


            // Return response containing the ID of the newly created customer
            return { customerId };
        }


        return { post };
    });

Leave a comment

Your email address will not be published. Required fields are marked *