Requirement
We need to display the files of any type from a particular folder in file cabinet to the select options in suitelet page.
Solution
A saved search created to fetch the files from a folder (internal id :1635) in file cabinet. Then this files can be populated in the options of the select field created in suitelet page.
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/record', 'N/search', 'N/ui/serverWidget'],
/**
* @param{record} record
* @param{search} search
* @param{serverWidget} serverWidget
*/
(recordsearch, serverWidget) => {
/**
* Defines the Suitelet script trigger point.
* @param {Object} scriptContext
* @param {ServerRequest} scriptContext.request - Incoming request
* @param {ServerResponse} scriptContext.response - Suitelet response
* @since 2015.2
*/
//Function created to define the search for to retrieve the contents of a folder in the file cabinet, the internal id of the folder is hardcoded here and it get the files of all type.
function fileSearch() {
var fileSearchObj = search.create({
type: "file",
filters:
[
["folder", "anyof", "1635"],
// "AND",
// ["filetype","anyof","JSON"]
],
columns:
[
search.createColumn({
name: "name",
sort: search.Sort.ASC,
label: "Name"
}),
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var fileSearchResult = fileSearchObj.run().getRange({
start: 0,
end: 1000
});
return fileSearchResult;
}
const onRequest = (scriptContext) => {
try {
//Method type = GET
if (scriptContext.request.method === 'GET') {
var fileSearchResult = fileSearch();
log.debug('File search result', fileSearchResult)
var fileM = form.addField({
id: 'custpage_filem',
label: 'FILE',
type: serverWidget.FieldType.SELECT,
align: serverWidget.LayoutJustification.RIGHT,
});
fileM.isMandatory=true
fileM.addSelectOption({
value: " ",
text: " ",
isSelected: true
})
//Populating the select box with the file names in the folder in the file cabinet
for (var f = 0; f < fileSearchResult.length; f++) {
fileM.addSelectOption({
value: fileSearchResult[f].getValue({name: "internalid", label: "Internal ID"}),
text: fileSearchResult[f].getValue({
name: "name",
sort: search.Sort.ASC,
label: "Name"
}),
})
}
}
} catch (e) {
log.debug('Error@Request', e)
}
}
return {onRequest}
});