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 };
});