RE: Create CSV file from Saved Search using SuiteScript (N/task module)
This is more of an informational post than a question. I spent a lot of time using the N/file module to generate CSV files from saved searches in SuiteScript, and it was a headache trying to make sure I escaped all the characters correctly so that the final CSV file would look like the CSV file that would be generated from clicking the button on the results page of a Saved Search.
After much time spent on this, it was brought to my attention that the N/task module creates CSV files from Saved Searches in just a couple lines of code. Not only is it much simpler, but it is also much faster than manually iterating through all the results of a Saved Search in SuiteScript and adding them to the CSV file one by one. The article on this topic and the code from that article are below.
https://ceanatech.com/2019/02/27/suitescript-2-0-why-you-should-check-out-n-task-searchtask/
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/task'],
/**
* @param {record} record
* @param {search} search
*/
function(task) {
var FILE_ID = 20;
var SEARCH_ID = 19;
function execute(scriptContext) {
var searchTask = task.create({
taskType: task.TaskType.SEARCH
});
searchTask.savedSearchId = SEARCH_ID;
searchTask.fileId = FILE_ID;
var searchTaskId = searchTask.submit();
}
return {
execute: execute
};
});
Thanks for linking our article on this! Saved so much time for our team as well hence I thought it’s worth sharing to everyone. 🙂
btgoligowski24
I think your post just saved me a ton of time and effort! Thanks!