0

I'm trying to insert/update multiple contacts (1000+) to google like that:

(in cycle:)

      if (contact == null) {
        contact = ContactsApp.createContact(first_name, last_name, email);
        group.addContact(contact);
      } else {
        contact.setFullName(first_name+' '+last_name);
        contact.setGivenName(first_name);
        contact.setFamilyName(last_name);
      }

It is working properly however in about 1 minute (100-130 contacts added) the script stops with:

Error Exceeded maximum execution time

Is there a way around this?

Thimotty
  • 35
  • 5

1 Answers1

2

Issue:

Apps Script has an execution time limit of 6 or 30 minutes, depending on your account. You are most likely reaching this limit.

Workaround:

To avoid this, you can split the actions you want to perform into different executions by setting a time-based trigger that will fire each successive execution after the previous one has finished. I guess your function has a loop, so you could follow these steps:

  • Find out how many iterations the script can go through before reaching the time limit (probably 6 minutes). Each execution will have to go through this amount of iterations before stopping.
  • Create the following time-based trigger at the end of your function: after(durationMilliseconds). Thanks to this, you can run whatever function you specify after the amount of milliseconds you indicate. After each execution, a trigger will be created to fire the next one.
  • Make your loop a for loop. Because you have to split the loop, you have to store the loop counter (lets call it i) somewhere (you could use PropertiesService at the end of each execution, or write it in the spreadsheet) and retrieve it at the beginning of the next, so that each in successive execution, the script knows where to resume the loop. See, for example, this answer if you don't know how to store and retrieve script properties.

Sample code (check inline comments):

function createContacts() {
  // Whatever your code outside the loop is
  var i_old = // Retrieve i stored in previous execution (from PropertiesService? Spreadsheet?) (should be 0 if first execution)
  var n_int = 100 // Number of iterations that can be done in a single execution without reaching time limit (change accordingly)
  var total = 1000 || yourVariable.length // Total number of iterations (change accordingly)
  // Loop starts at previous i store until it reaches the specified number of iterations for one execution or reaches the total number:
  for(var i = i_old; i <= i_old + n_int && i <= total; i++) {
    // Whatever your code inside the loop is
  }
  // Store i somewhere (PropertiesService? Spreadsheet?)
  if (i <= total) { // Create trigger if i hasn't reach the total number of iteration
    ScriptApp.newTrigger("createContacts")
    .timeBased()
    .after(1000 * 60) // This fires the function 1 minute after the current execution ends. Change this time according to your preferences
    .create();  
  }
}

Note:

  • Please notice that only 1,000 or 2,000 contacts (depending on the account type) can be created daily.

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27