Call a RESTlet and Display Results

This sample is part of a multi-script sample. To see the entire code sample, please see Retrieve Employee Information Using a Suitelet and a RESTlet Script with a Defined Content-Type Header.

The following sample is a Suitelet script calling a RESTlet script through https.requestRestlet(). The RESTlet returns an application/json value with the employee name. For more information about https.requestRestlet(), see https.requestRestlet(options).

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

          /**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/https'],
    
    (https) => {
        /**
         * Defines the Suitelet script trigger point.
         * @param {Object} scriptContext
         * @param {ServerRequest} scriptContext.request - Incoming request
         * @param {ServerResponse} scriptContext.response - Suitelet response
         * @since 2015.2
         */
        const onRequest = (scriptContext) => {
            // Get the employee ID from the parameters
            const employeeId = scriptContext.request.parameters.employeeId || '0';

            // Define output variable
            let output = '';

            // Retrieve the employee name 
            const restletResponse = https.requestRestlet({
                body: JSON.stringify({ employeeId }),
                deploymentId: 'customdeploy_samples_rs_requestrestlet',
                scriptId: 'customscript_samples_rs_requestrestlet',
                headers: { 'Content-Type': 'application/json' },
                method: 'POST'
            });

            // Check if the RESTlet call is successful
            if (restletResponse.code === 200) {
                if (restletResponse.body !== '') {
                    // Parse the RESTlet response
                    const restletBody = JSON.parse(restletResponse.body);

                    // Write the suitelet output
                    output = `Employee Name is: ${restletBody.name}`;
                } else {
                    // Write error message.
                    output = 'No employee found.';
                }

            } else {
                // Write error message.
                output = 'Unexpected error. Please check script logs.';
            }

            // Write output to response object.
            scriptContext.response.write(output);
        }

        return {onRequest}

    }); 

        

General Notices