This article is relevant if you are want to dynamically create a list of file names for user selection. It also helps explain concepts on complex search columns lookups.
Background
Reviewing NetSuite’s Record Browser, you may notice that a “File” object is not available. That is because it is really not a “record”. You use NetSuite’s nlapiLoadFile(id) function to load a file versus nlapiLoadRecord syntax.
However, you may have a list of files that you want to present to the user for some type of operation. Suppose that you have designated a particular folder in the NetSuite File Cabinet to hold some special files. How do you get a list of those files?
SuiteScript Solution for Filename Dropdown List
The following code snippet can be plugged into a UserEvent BeforeLoad function to help draw a file dropdown list. The key here is to perform a search on the Folder record object. All files have a parent folder ID. We basically are going to use the join functionality of a saved search to get to the files in the folder.
// pass in the internal id of the file's parent folder var folder = 415 // the folder ID we care about var filters = new Array(); filters[0] = new nlobjSearchFilter('internalid', null, 'is', folder); // get two columns so we can build a drop down list: file name and file internal ID var columns = new Array(); var filename = new nlobjSearchColumn('name', 'file'); var fileid = new nlobjSearchColumn('internalid', 'file'); //use this syntax so we can pass in the object reference to get the file info, not the //text representation of column lookup columns[0] = filename; columns[1] = fileid; // perform the search and loop through the findings var searchResult = nlapiSearchRecord('folder', null , filters , columns); if(searchResult) { for (var i = 0 ; i < searchResult.length; i++) { var f = searchResult[i]; //add values to the "template" dropdown field created earlier in the UI //leveraging our getValue(<object reference>) syntax template.addSelectOption(f.getValue(fileid), f.getValue(filename)); }; };
A Word About Search Column References
The typical way to reference columns from a search is to enter something like getValue(‘internalid’). NetSuite’s sample code shows syntax like this all the time. This technique works if the column is not complex. But in our case, we are referencing joined columns which is less simple. The same thing can happen if you create complex formula columns.
What you need to do is reference the explicit column definition through a variable pointer to your column array elements. See comments in the code snippet.
Get Help
NetSuite was designed to be extended to meet unique business requirements. In the hands of the right actors, you can expand your capacity to automate business process, increase revenue and lower costs. Contact us if you would like to get more from your NetSuite investment.
Gracias…
How can I list folders within folder? Thanks!
Hello Georgi,
The search is of type ‘folder’. Thus, if you do not reference any of the “files” (this is the join part of the search), all you will get back is a set of other folders. Thus instead of this, var filename = new nlobjSearchColumn(‘name’, ‘file’); use this var filename = new nlobjSearchColumn(‘name’);
Marty
Hi Marty,
Is it possible to retrieve a file on an external suitelet post without using the NetSuite form.addField?
In other words, I want to use a simple and retrieve that file on form post using request.getFile.
Sorry, looks like some of my previous post didn’t go through. Here’s the edited version:
Is it possible to retrieve a file on an external suitelet post without using the NetSuite form.addField?
In other words, I want to use a simple “input type=file” and retrieve that file on form post using request.getFile.
Here is an article for how you can create SuiteLet’s to deliver information. It talks about solving a cross domain challenge, but really, a SuiteLet is an entry point into NetSuite that you can deliver all kinds of data. What is nice is you don’t need to be authenticated.
Finally, remember all NetSuite files can be marked with a switch to “allow anonymous” which then gives you a hyperlink to download the file. No programming required.
Marty
Hi Quentin,
I think you were asking to go another direction. In SuiteScript 2.0, here is the pattern:
var fileField = form.addField({
id: 'file',
label: 'File',
type: ui.FieldType.FILE
})
//Save any files uploaded to the cabinet
if (request.files.file)
{
//Set the location/folder for saving the file
request.files.file.folder = runtime.getCurrentScript().getParameter({
name: 'custscript_pri_trx_app_file_cabinet_loc'
});
//Set to available without login
request.files.file.isOnline = true;
//Force a unique name by timestampting the file
request.files.file.name = Date.now() + '_' + request.files.file.name;
//Save the file
var internalID = request.files.file.save();
}
Marty, I’m making a custom “app” via external suitelets and part of the functionality is to allow the user to submit files. The UI for this app is 100% custom, and so I can’t use the NetSuite form.addField anything because there’s no way to put these fields exactly where I want them on the page (unless I’m wrong about this?). As such, I use my own fields using standard HTML inputs and, in some cases, DIVs. The only exception is the file field. On the post action of the form, NetSuite will not return any object from a standard input type=file – it has to be a form.addField input. Because of this I have to create a separate suitelet just to handle the file upload, which is annoying and doesn’t look good due to lack of design control. Hence, I’m still wondering if I can somehow grab the file on the post action from a standard HTML input…..
Hi Quentin,
Good question. Did you try to and walk the response object through iterating its properties and elements to see if you can get to any specific information about the file?
Marty
Hi Marty,
Is there a possibility to get all the list of the files from all the folders.
Hello Michaela,
I am not sure I understand your question. Are you having trouble with native saved search?
Marty
Hi Marty, is it possible to get the pdf file from filecabinet going to newly created custom body field in invoice using SuiteScript? can you guide me how to do that.? thanks.
Yes, you can. But the key is to know how to search for it. You can search the message list off the transaction list. Once you have that, you can grab the ID for your purposes. We also use our CRE tool to link searches together to get outcomes without programming.
Hi Marty,
Is this possible to create this in SuiteScript 2.0 Restlet?
Nothing is returned when modifying the example to work in SuiteScript 2.0 running it in a restlet.
Hello Cooper,
I find first, I get my program to work without being a Restlet. Then, I encapsulate it into a Restlet function call. You should be able to return results so long as you are shaping the output in a JSON formats. See this article for simple Restlet 2.0 return definition.
https://blog.prolecto.com/2017/10/14/download-netsuite-oauth-token-based-authentication-sample-node-js-program/
Marty