we are trying to make a script to handle all our s...
# suitescript
s
we are trying to make a script to handle all our summary for map/reduce script we are passing the summary to a function but it seems to only get the first iteration
Copy code
function summarize(summary) {let body = emailMailer.createEmailObjectFromData(summary, {
        type,
      });
      emailMailer.sendEmail(body, type);
    }}

 function createEmailObjectFromData(summary, typeHeaders) {
    let errorCount = 0;
    let tableRow = "";
    summary.output.iterator().each(function (key, error) {
      var errorObject = JSON.parse(error);
      errorCount++;
      tableRow += createTableRow(errorObject, errorCount);
    });
    let response =
      errorCount != 0 ? createBodyEmail(tableRow, typeHeaders) : errorCount;
    log.audit("errorCount", errorCount);
    return response;
  }
e
All of NetSuite's iterators behave the same. Your callback must return a truthy value in order for the iterator to continue. Returning a falsy value makes the iterator stop. Your iterator callback has no return statement, which is the same as returning
undefined
, and
undefined
is falsy.
Copy code
summary.output.iterator().each((key, error) => {
  // ... callback logic
  return true; // continue iterating
})
Other iterators behave the same - like the
each()
method from
N/search
or
N/query
FWIW here's a general utility function I use in my
summarize
stages to compile all the errors from all other stages:
Copy code
const parseErrors = (summary) => {
    let errors = []

    if (summary.inputSummary.error) {
      errors.push(summary.inputSummary.error)
    }

    summary.mapSummary.errors.iterator().each((k, e) => errors.push(e))
    summary.reduceSummary.errors.iterator().each((k, e) => errors.push(e))

    return errors;
  }
Note that because my arrow functions in my
each()
callbacks don't use braces, they are returning the result of
errors.push()
which is why iteration continues without an explicit
return
statement
s
thanks
coming from a background in ruby where everything is implicit return this trips me up sometimes to have explicit
return
statement on non arrow fuctions
e
Yeah I can see how that'd be troublesome; lots of habits to re-form 🙂
s
yup especially when i write code in both
🤣 1
I wish there was a way to know if there is anything inside with out calling the function/callback
Copy code
let data  = summary. output;