Seems like NetSuite doesn't like sending one email...
# suitescript
m
Seems like NetSuite doesn't like sending one email containing more than 10 attachments. My Scheduled script will run every 30mins so I am trying to process 10 attachments at a time, but my loop isn't quite working. My first search loop works fine, but the for loop with the count runs within the search loop and runs each line. I need a way to check that it's added 10 files then stop.
Copy code
var file_count = file_search.runPaged().count;
 file_search.run().each(function (result) {
     internal_id = result.getValue({
         name: 'internalid',
     });

     file_id = result.getValue({
         name: 'internalid',
         join: 'file'
     });
     log.debug({title: 'File ID', details: file_id});
     log.debug({title: 'Number of files found', details: file_count});
     
     for (var i = 1; i < file_count; i++) {
         log.debug({title: "Current Count", details: i})
         if (i < 11) { //attachment limit is 10
             if (file_id) {
                 file_collection.push(file.load({
                     id: file_id
                 }))
             }
         }
     }
     log.debug({title: "Total Count", details: i})
     const folder_id = 1933298;
     var _file_obj = file.load({
         id: file_id
     });
     log.debug("Move PDF File To Archived Folder");
     _file_obj.folder = folder_id;
     _file_obj.save();
     return true;
 });

Testing with 11 files. I expect it to attach the first 10 then stop.
b
thats usually done at the condition for the for loop
Copy code
(var i = 1; i < file_count; i++)
that starts i at 1, and stops when i is greater than the file count
you can add a condition to also make it stop when i is greater than 10
id normally warn that starting i at 1 is unusual, but the entire loop is weird
you dont actually use i anywhere inside the for loop
m
Thanks. Based on what you said the for loop looks like this now:
Copy code
for (var i = 0; i < file_count; i++) {
                    log.debug({title: "Current Count", details: i})
                    if (i === 11) { //attachment limit is 10
                        break;
                        if (file_id) {
                            file_collection.push(file.load({
                                id: file_id
                            }))
                        }
                    }
                }
But I still have the problem - I need to get out of the for loop as I need to continue running the .each loop. But right now the for loop completes before the next .each iteration starts.
b
still dont see the point of the loop
you are pushing the same file into your array multiple times
m
I only used the loop to get a count. Can I get a count from the .each?
b
your not counting what you think you are counting
imagine you have 5 search results
how many files are you loading and putting into your file_collection
m
I see what you mean. I've now removed the for loop and instead used a counter inside the .each and now I can create a condition using the counter.