/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define([‘N/record’, ‘N/file’, ‘N/search’, ‘N/log’],
/**
* @param{record} record
*/
(record, file, search, log) => {
const apiMethods = {
fetchEmployeeDetails() {
let requestBody = rootContext.body;
log.debug(‘requestBody’, requestBody);
const customlistSearch = search.create({
type: ‘customlist_jj_time_type’,
//filters: [[“name”, “contains”, searchTerm]],
columns: [
search.createColumn({ name: ‘internalid’, label: ‘Internal ID’ }),
search.createColumn({ name: ‘name’, label: ‘Name’ })
]
});
const results = [];
customlistSearch.run().each(result => {
results.push({
id: result.getValue({ name: ‘internalid’ }),
name: result.getValue({ name: ‘name’ })
});
return true;
});
log.debug(“results”, results)
return {
status: ‘SUCCESS’,
reason: ‘FETCHED_EMPLOYEE_DETAILS’,
data: results
};
}
}
const rootContext = {
scriptContext: null,
method: null,
header: null,
parameters: null,
body: null,
/**
* @description To initialize the export Object with the Suitelet methods and parameters and body
* @param {Object} scriptContext
*/
init(scriptContext) {
this.scriptContext = scriptContext;
this.method = scriptContext.request.method;
this.headers = scriptContext.request.headers;
this.parameters = scriptContext.request.parameters;
this.body = scriptContext.request.body;
this.parseJSON();
},
parseJSON() {
try {
if (this.body) {
this.body = JSON.parse(this.body);
}
}
catch (err) {
this.body = “CANNOT PARSE BODY —“ + this.body;
}
},
/**
* @description To route request based on API Type
*/
routeRequest() {
if ((this.parameters.apiType)) {
switch (this.parameters.apiType) {
case “fetchEmployeeDetails”:
return apiMethods.fetchEmployeeDetails();
default:
return { status: ‘FAILURE’, reason: ‘INVALID_APITYPE’, data: null };
}
}
},
/* @description Structures and sens the response. All response will be send from this common point
* @param STATUS – It will be either Success or Failure
* @param REASON – Reason Code
* @param DATA – Data to be passed if any
* @returns {boolean}
*/
sendResponse(responseObj) {
let returnVal
const wrapInEscapedBody = (data) => {
return encodeURIComponent(JSON.stringify(data));
};
const unwrapInEscapedBody = (data) => {
return JSON.parse(decodeURIComponent(data));
};
if (this.method === ‘POST’) {
returnVal = {
status: responseObj.status,
reason: responseObj.reason,
data: wrapInEscapedBody(responseObj.data)
}
return this.scriptContext.response.write(`${JSON.stringify(returnVal)}`, true)
}
}
}
/**
* 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) => {
rootContext.init(scriptContext);
return rootContext.sendResponse(rootContext.routeRequest())
}
return { onRequest }
});