i need to export the saved searches into the file ...
# suitescript
v
i need to export the saved searches into the file cabinet thru scripting. i dont have any idea about this, could anyone please help me?
r
You want the search result of those searches in a CSV, I assume ?
There are two ways. Use task modhle pass on the search id and the path. (checkout task.SearchTask in help center) Or run each search create CSV by urself by iterating through search result and then save that file in the file cabinet.
v
Or run each search create CSV by urself by iterating through search result and then save that file in the file cabinet. could you please explain this in detail?
r
have an array of all save searches id, iterate through that array, load the search, iterate the result, create the csv, and store it in a folder in file file cabinet. All that is being done automatically through the task module (task.SearchTask) which you will find easier to implement. Creating a CSV on your own, you have to handle some scenarios like comma coming inside your results, etc.
v
i tried to have all the saved searches in an array, but i could not find it success. do you have any code?
r
what is the error you are having?
v
no i dont even find the code itself
r
which script type you are using?
v
scheduled script
r
how many searches are there in total you need to export? and how frequently you are exporting them? Also share your code whatever you have written so far.
v
define(['N/task','N/record','N/search'], function(task, record, search) { function execute(context) { log.debug('cont',context); var sSearch = search.load({ id : 3597 }); log.debug('dfs',sSearch); var sSearchLength = sSearch.run().getRange({ start: 0, end: 999 }).length; log.debug('sSearchLength',sSearchLength); for(var i = 0; i < sSearchLength; i++) { var searchTask = task.create({ taskType: task.TaskType.SEARCH }); searchTask.savedSearchId = 3597; searchTask.fileId = 1419679; var searchTaskId = searchTask.submit(); log.debug('id',searchTaskId); } } return { execute : execute }; } );
you can use the below code Add as many searches you want along with the file names in the searchMap dictionary. Note: Different file Name for each search otherwise it will overwrite the existing file.
Copy code
define(['N/task','N/record','N/search'],
    function(task, record, search) {
        function execute(context) {
            const searchMap = {3597 : 'file1'} //searchId : filename
            Object.keys(searchMap).map(function(each) {
                let searchTask = task.create({
                    taskType: task.TaskType.SEARCH
                });
                searchTask.savedSearchId = each;
                let path = `ExportFolder/${searchMap[each]}.csv`;
                searchTask.filePath = path;
                let searchTaskId = searchTask.submit();
                log.debug("searchTaskId",searchTaskId)
            })
        }
        return {
            execute : execute
        };
    }
);