Dave Garver
Dave Garver

Reputation: 13

Google Script: Cannot retrieve (line 9, file "Code")

I am attempting to set up my first Google Script. It should remove a label from the set of emails which match my search criteria, but when I run the script I get this error:

Cannot retrieve (line 9, file "Code")

My code:

function ArchiveEmails() {
  var misc_reps = GmailApp.search("from:([email protected]) \"Source: misc_reports\" \"The file was successfully processed\"");
  var imp_tms_processing = GmailApp.getUserLabelByName('imp-tms-processing');


  for (var i=0; i<misc_reps.length; i++) {
    var misc_rep = misc_reps[i];
    var id_string = misc_rep.getId(); //use to confirm specific email found in debugging
    misc_rep.removeLabel(imp_tms_processing); // line 9
  }  
}

I don't know where to go from here; I can't find any documentation on this issue.

Upvotes: 1

Views: 927

Answers (2)

klg
klg

Reputation: 21

The dashes to replace the spaces in labels is necessary for search criteria but not needed for this function, getUserLabelByName. I've just spent a couple of days trying to use this specific function with no luck. As soon as I removed the dashes and used the actual label name, it worked fine.

Upvotes: 2

SharkAlley
SharkAlley

Reputation: 11659

It means Gmail could not find the label. You would think that error would occur on this line:

var imp_tms_processing = GmailApp.getUserLabelByName('imp-tms-processing');

but for some reason it doesn't throw an error until you actually try to do something with the label.

Log in to the Gmail account and make sure that your label exists. If your label is nested under a parent label, use the form:

GmailApp.getUserLabelByName("parent/child");

Upvotes: 5

Related Questions