I have this code below:
reportPDF = doc.getAs('application/pdf')
reportPDF.setName('Automated report - '+ rows[0][0] + ".pdf");
var file1 = destinationFolder.createFile(reportPDF);
var folder = DriveApp.getFolderById("19Rv2f-Ud4Ncdzu-1MT1vUmh49pOKfKZX");
var file2 = folder.getFilesByName("test.pdf");
DriveApp.getFileById(doc.getId()).setTrashed(true);
if(file2.hasNext()){
emails.forEach(function(email) {
MailApp.sendEmail(email, "Automated Report - " + rows[0][0], "Hello!", {
name: 'Good Practices Report',
attachments:
[
file1.getAs(MimeType.PDF),
file2.next().getAs(MimeType.PDF)
]
});
})
}
With this code, I should receive an email containing the two attachments (file1 and file2). However, when I run it, I get the following error:
Exception: Cannot retrieve the next object: iterator has reached the end. (line 158, file "Email")
In this folder, I just have one file with the name test.pdf, but the code still still accusing error on line 158 : file2.next().getAs(MimeType.PDF).
Does anyone knows what might be happening?