Is it possible to schedule a suite script to run a...
# administration
j
Is it possible to schedule a suite script to run a saved search to export that search results to the file cabinet?
e
Yes
j
How can I do this
e
Here’s what ChatGPT came up with. Looks pretty solid.
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType ScheduledScript
 */
define(['N/search', 'N/file'], function(search, file) {
  function execute(context) {
    // Replace 'savedSearchId' with the ID of the saved search you want to run
    var savedSearch = search.load({
      id: 'savedSearchId'
    });

    // Run the saved search and get the search results
    var searchResults = savedSearch.run();

    // Create a new file in the file cabinet
    var fileObj = file.create({
      name: 'searchResults.csv',
      fileType: file.Type.CSV,
      contents: searchResults.getRange({
        start: 0,
        end: 1000
      }).map(function(result) {
        return result.getAllColumns().map(function(column) {
          return result.getValue(column);
        }).join(',');
      }).join('\n')
    });

    // Save the file in the file cabinet
    fileObj.save();
  }

  return {
    execute: execute
  };
});
j
Pefect…Let me give this a shot
Thank you very much
@Bill Duncan
b
e
I’ve never used the task module. Thanks for sharing.
j
I added this script and this is the error I got….Environment: Production Date & Time: 01/08/2023 03:28 pm Execution Time: 0.00s Script Usage: 0 Script: Avi CC Search Type: Scheduled Function: execute Error: JS_EXCEPTION org.mozilla.javascript.EcmaError: TypeError: Cannot find function getAllColumns in object search.Result. (/SuiteScripts/avi_cc_search.js#23)
Any thoughts? Should I use the internal id for the saved search or the script id here?
e
You should be able to use either. That’s not where the problem is occurring though. I suspect you might be defaulting to 2.0 instead of 2.1. Before you go further, I’d recommend using the recommendation by @battk . That task feature is prebuilt to export a search out to CSV and you can eliminate a good portion of your code.
j
Yeah I looked at that but I need to be able to come get that file within Netsuite in order to pick it up with a Boomi integration.
e
How does your Boomi integration get the file? Does it pull it from the File Cabinet?
j
Yeah it can query the file cabinet within netsuite. It can hit a local directory but that would be an additional connection that I don’t have
e
So, both of the recommended solutions would put a CSV file into the File Cabinet. I’m not clear on why the second recommendation of using the task module would be a problem.
j
I am going to try the task module….I didn’t realize it put it on the file cabinet
👍 1
b
beyond the obvious things like errors in the code (getAllColumns is ss1, folder is missing), the code given here would need to be rewritten if you have more than 1000 rows or if any of your data contained reserved characters used by csv (quotes, commas, and new lines)
j
ok yeah i don’t see the search being more than 1000 rows.
How would i specify the folder i want to put it in?
b
j
ok perfect….thank you so much