Daniel Yue
Daniel Yue

Reputation: 1

Exception: Cannot retrieve the next object: iterator has reached the end. Line 26 Error

function sendMail() {
  var ofo = 1;
  var restaurant = 2;
  var name = 3;
  var email = 4;
  var sent = 5;

  var emailTemp = HtmlService.createTemplateFromFile("Email");

  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Invalid Login MM Pull Internal");

  var data = ws.getRange("A2:F" + ws.getLastRow()).getValues();

  data.forEach(function(row){

    emailTemp.ofo = row[ofo];
    var file = DriveApp.getFilesByName("Guide to Resetting Online Delivery Passwords.pdf");
    var htmlMessage = emailTemp.evaluate().getContent();
    var startRow = 2;
    for (var i = 0; i < data.length; ++i){
    if (row[sent] !== "EMAIL_SENT"){
        GmailApp.sendEmail(row[email], "Otter Onboarding: Invalid " + row[ofo] + " Logins Submitted", 
        "Your email doesn't support HTML, please trying using another browser.",
        {name: "The Otter Onboarding team", 
        htmlBody: htmlMessage, replyTo: "[email protected]",
        attachments: [file.next().getAs(MimeType.PDF)]
        });
        ws.getRange(2 + i,6).setValue("EMAIL_SENT");
      };
    };
     });
}

I am trying to attached this PDF to my emails but it keep saying line 26 there is an error: Exception: Cannot retrieve the next object: iterator has reached the end.

Upvotes: 0

Views: 148

Answers (1)

Yuri Khristich
Yuri Khristich

Reputation: 14537

Just a guess. Instead of this:

var file = DriveApp.getFilesByName("Guide to Resetting Online Delivery Passwords.pdf");

...

attachments: [file.next().getAs(MimeType.PDF)]

You probably need this:

var file = DriveApp.getFilesByName("Guide to Resetting Online Delivery Passwords.pdf").next();

...

attachments: [file.getAs(MimeType.PDF)]

The getFiles() gives you an iterator (something similar with an array). The method .next() gives you a first (only, actually) element of the iterator.

Upvotes: 1

Related Questions