Send a POST Request from a Suitelet to RESTlet

Scenario

The user wants to create a Suitelet that submits data into a RESTlet which creates a Customer record.

Solution

Create 2 scripts. One is a Suitelet that prepares the Customer data and calls the RESTlet. The other is a RESTlet that creates the Customer record with the data handed by the Suitelet.

Suitelet:

SuiteScript 1.0

function requestFromForm(request, response) {
    if (request.getMethod() == 'GET') {
        //create a form where users could enter data
        var form = nlapiCreateForm('Simple Form');
        var companyname = form.addField('companyname', 'text', 'Company:');
        form.addSubmitButton('Submit');
        response.writePage(form);
    } else {
        //when form is submitted create and send the request to the Restlet
        try {
            //URL of Restlet
            var url = "https://rest.netsuite.com/app/site/hosting/restlet.nl?script=108&deploy=1";

            //Authorization header
            var authorization = "NLAuth nlauth_account=TSTDRV12345,nlauth_email=email@netsuite.com, nlauth_signature=*******,nlauth_role=3";

            //Create the request headers
            var headers = new Array();
            headers['Content-Type'] = 'application/json';
            headers['Authorization'] = authorization;
            headers['User-Agent-x'] = 'SuiteScript Call';

            //obtain the values entered by the user
            var companyname = request.getParameter('companyname');
            var isperson = false;

            //format the data to be submitted; for this instance I used JSON
            var jsonString = "{"companyname":"" + companyname + "","isperson":" + isperson + "}";

            //send the request to the restlet and retrieve the response
            var output = nlapiRequestURL(url, jsonString, headers, null, "POST");
            response.write(output.getBody());
        } catch (e) {
            throw nlapiCreateError('SUITELET_ERROR', e.name + ' ' + e.message, false);
        }
    }
}
Copy

SuiteScript 2.0

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
define(["N/ui/serverWidget","N/search", "N/error"],
function(serverWidget, search, error) {
    function requestFromForm(context) {
        if (context.request.method == 'GET') {
            //create a form where users could enter data
            var form = serverWidget.createForm({
                title: 'Simple Form'
            });
            var companyname = form.addField({
                id: 'custpage_companyname',
                type: serverWidget.FieldType.TEXT,
                label: 'Company:'
            });
            form.addSubmitButton({
                label: 'Submit'
            });
            response.writePage({
                pageObject: form
            });
        } else {
        //when form is submitted create and send the request to the Restlet
            try {
                //URL of Restlet
                var url = "https://rest.netsuite.com/app/site/hosting/restlet.nl?script=108&deploy=1";

                //Authorization header
                var authorization = "NLAuth nlauth_account=TSTDRV12345,nlauth_email=email@netsuite.com, nlauth_signature=*******,nlauth_role=3";

                //Create the request headers
                var headers = new Array();
                headers['Content-Type'] = 'application/json';
                headers['Authorization'] = authorization;
                headers['User-Agent-x'] = 'SuiteScript Call';

                //obtain the values entered by the user
                var companyname = context.request.parameters.companyname;
                var isperson = false;

                //format the data to be submitted; for this instance I used JSON
                var jsonString = "{"companyname":"" + companyname + "","isperson":" + isperson + "}";

                //send the request to the restlet and retrieve the response
                var output = https.post({
                    url: url,
                    body: jsonString,
                    headers: headers
                });
                response.write({
                    output: output.body
                });
            } catch (e) {
                throw error.create({
                    name: 'SUITELET_ERROR',
                    message: e.name + ' ' + e.message,
                    notifyOff: false
                });
            }
        }

    } return {
        onRequest: requestFromForm
    }
});
Copy
 

RESTlet:

SuiteScript 1.0

function RestletPost(data) {
    //create customer record
    var customer = nlapiCreateRecord('customer');

    //set values of the record depending on the values submitted to the Restlet
    if (data.isperson == false) {
        customer.setFieldValue('isperson', 'F');
    } else {
        customer.setFieldValue('isperson', 'T');
    }
    customer.setFieldValue('companyname', data.companyname);
    customer.setFieldValue('billpay', 'F');

    //submit record to NetSuite
    var id = nlapiSubmitRecord(customer);

    //create an object that would contain the response to be sent
    var output = new Object();
    output.id = id;

    //send response back
    return output;
}
Copy

SuiteScript 2.0

/**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
define(['N/record'],
function(record) {
    function doPost(requestBody) {
        //create customer record
        var customer = record.create({
            type: record.Type.CUSTOMER
        });

        //set values of the record depending on the values submitted to the Restlet
        if (requestBody.isperson == false) {
            customer.setValue({
                fieldId: 'isperson',
                value: 'F'
            });
        } else {
            customer.setValue({
                fieldId: 'isperson',
                value: 'T'
            });
        }
        customer.setValue({
            fieldId: 'companyname',
            value: requestBody.companyname
        });
        customer.setValue({
            fieldId: 'billpay',
            value: false
        });

        //submit record to NetSuite
        var id = customer.save();

        //create an object that would contain the response to be sent
        var output = new Object();
        output.id = id;

        //send response back
        return output;

    } return {
        post: doPost
    }
});
Copy


Leave a comment

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