Retrieve Employee Information Using a Suitelet and a RESTlet Script with a Defined Content-Type Header
This sample consists of a Suitelet script calling a RESTlet script. The RESTlet result dictates the output of the Suitelet.
Call a RESTlet and Display Results
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).
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.
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}
});
Query Employee Name and Return Data in JSON Format
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.
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 };
});