RESTlet Script Samples

This section of the RESTlet Code Samples catalog includes the following script types code samples:

Query Employee Name and Return Data in JSON Format

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 RESTlet queries employee information based on the employee ID from the RESTlet request body. This RESTlet script expects the request to be have a Content-Type Header of application/json. The RESTlet response is also expected to have the same Content-Type Header.

Note:

This sample script 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 Debugger.

            /*
 * Copyright (c) 2022, Oracle and/or its affiliates.
 */

/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */
define(['N/scriptTypes/restlet', 'N/query'], function (restlet, query) {
    // Restlet entry point
    const post = function (requestBody) {
        // Define the output variable
        let returnValue = '';

        // Create the Employee Query
        const qObj = query.create({
            type: query.Type.EMPLOYEE
        });

        // Check if the requestBody and the employeeId field is defined
        if (!!requestBody && !!requestBody.employeeId) {
            // Define the query condition for employee ID field in Employee record
            qObj.condition = qObj.createCondition({
                fieldId: 'id',
                operator: query.Operator.EQUAL,
                values: requestBody.employeeId
            });

            // Add the desired columns in the Query
            qObj.columns = [
                qObj.createColumn({
                    fieldId: 'firstname'
                }),
                qObj.createColumn({
                    fieldId: 'lastname'
                })
            ];

            // Retrieve the results
            const results = qObj.run().asMappedResults();
            for (let i = 0; i < results.length; i++) {
                const result = results[i];
                returnValue = {
                    name: result.firstname + ' ' + (result.lastname || '')
                };
            }
        }
        
        return returnValue;
    }

    return { post };
}); 

          

Query Employee Supervisors and Create Response with Content-Type Header

The following sample queries employee supervisors, and then sets the RESTlet response Content-Type header to application/json.

Note:

This sample script 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 Debugger.

            /**
 * @NApiVersion 2.1
 * @NScriptType RESTlet
 */
define(['N/scriptTypes/restlet', 'N/query'], 
    function(restlet, query) {
   
    // RESTlet entry point
    const get = function (requestParams) {
        // Create Employee query
        const qObj = query.create({
            type: query.Type.EMPLOYEE
        });
        // Set query condition for the Supervisor field on the employee record, it should not be empty
        qObj.condition = qObj.createCondition({
            fieldId: 'supervisor',
            operator: query.Operator.EMPTY_NOT
        });
        // Add supervisor value in the query results
        qObj.columns = [qObj.createColumn({
            fieldId: 'supervisor'
        })]

        const returnArray = [];
        
        // Retrieve the results
        const results = qObj.run().results;
        for (var i = results.length - 1; i >= 0; i--) {
            const obj = results[i];
            returnArray.push(obj.values[0]);
        }
        
        // Create a RESTlet custom response to set the Content-Type header to JSON object
        const response = restlet.createResponse({ 
            content: JSON.stringify(returnArray),
            contentType: 'application/json'
        });

        log.debug('response', JSON.stringify(response))

        return response;
    }

    return { get };
}); 

          

Related Topics:

General Notices