```const file = file_search.run().getRange({ s...
# suitescript
m
Copy code
const file = file_search.run().getRange({
    start: 0,
    end: 10
}).forEach(getFiles);

try {
    log.debug("Sending Email");
    email.send({
        author: 129272,
        recipients: '<mailto:test@test.com|test@test.com>',
        subject: "Note",
        body: "Delivery Note Attached",
        attachments: [file],
        cc: null
    });
} catch (e) {
    log.error({title: 'Catch Block', details: e.toString()});
}

function getFiles(result) {
    const file_internal_id = result.getValue({
        name: 'internalid',
        join: 'file'
    });
    log.debug({title: "File Internal ID", details: file_internal_id});
    return true;
}
Why is this throwing TypeError: Cannot call method "hasOwnProperty" of undefined in my catch block?
d
the
toString()
function on the error object must have a
hasOwnProperty()
call on something that's undefined within the error object 😕 Not really an explanation of precisely why, but a solution would be to use
JSON.stringify(e)
instead of
e.toString()
m
the issue must be the sendEmail as I can get into the try and then it fails. I think it's this parameter: attachments: [file] I'm trying to attach the pdf files in the folder
Added a log:
Copy code
forEach(getFiles);
log.debug({title: "File", details: file});
file is blank. How should I get the files using the forEach?
g
aside : i often do this if i don’t want to rethrow the error. if i do i’ll localize the de-structure
Copy code
...
catch({ message, stack}){
  log.error({ title : 'something helpful', details : {message, stack}}); 
}
How should I get the files using the forEach?
to get just the contents you’ll need to
getContents()
it from file record. to get attributes of the file without loading full file include that in your search. to attach it to an email as your code implies, you need
file.File[]
array so you’ll have to use a method (e.g.
file
(N/file) ,
render
(N/render) ) that returns that against your accumulated ids. probably for you
file.load
i think you actually want this though (and maybe
forEach
was a typo? in
each
loop accumulate ids or file.File records (by loading them) and then pass your accumulator to
email.send
https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_454656921386.html
b
that error is from one of netsuite's modules
usually because you passed the wrong object in
you dont want to catch the error, netuite's error reporting mechanisms are usually good at giving you the stack trace so you can find the line that is throwing the error
without a stacktrace, the guess is that file is undefined, mostly because forEach does not have a return value
m
Thanks. I've redone this but while a single email is sent, I don't get all the attachments. I tested this with 3 files and only one file was attached:
Copy code
function execute(context) {

    var internal_id, file_obj, file_id, file_name, size;
    const file_search = search.load({
        id: 'customsearch_email_del_notes'
    });

    var file_count = file_search.runPaged().count;
    log.debug({title: 'Number of files found', details: file_count});
    file_search.run().each(function (result) {
        file_id = result.getValue({
            name: 'internalid',
            join: 'file'
        });
        file_name = result.getValue({
            name: 'name',
            join: 'file'
        });

        file_obj = [];
        file_obj.push(file.load({
            id: file_id
        }))
        log.debug({title: "File Array Object", details: file_obj + " " + "Length:" + " " + file_obj.length});
        return true;
    });
    try {
        log.debug("Sending Email");
        email.send({
            author: 129272,
            recipients: '<mailto:test@test.com|test@test.com>',
            subject: "Delivery Note",
            body: "Delivery Note Attached",
            attachments: file_obj,
            cc: null
        });
    } catch (e) {
        log.error({title: 'Catch Block', details: e.JSON.stringify()});
    }
}
g
file_obj
is being reset to an empty array in every iteration. instead use your
var
declaration to establish it a default value of
[]
and then each loop can
push
to it.
👍 1
m
Thanks, that was it - all good now