title: Google Apps Script Contacts Services error
body: <p>First, I know, there is a <a href="https://stackoverflow.com/questions/10954075/unexpected-exception-upon-serializing-continuation">similar question</a> but it's just similar and not the same :)</p>
  
  <p>This script import Google Contacts -> <a href="https://developers.google.com/apps-script/service_contacts" rel="nofollow noreferrer">Contacts Sevices</a>, in a spreadsheet.</p>
  
  <pre><code>function onOpen()
  {
    var menuItems = [{name: "Gmail Contact Import", functionName: "getContacts"}];
    SpreadsheetApp.getActiveSpreadsheet().addMenu("import", menuItems);
  }
  
  function getContacts()
  {
    var groupName = "MyGroup";
    var contacts;
    var sheet = SpreadsheetApp.getActiveSheet();
  
    if (groupName) {
      var contactGroup = ContactsApp.findContactGroup(groupName);
  
      if (contactGroup) {
        contacts = contactGroup.getContacts();
      }
    } 
    else {
      contacts = ContactsApp.getAllContacts();
    }
  
    if (!contacts) {
      Browser.msgBox("no contacts found!");
      return;
    }  
  
    sheet.clear();
  
    sheet.getRange("A1").setValue("forename");
    sheet.getRange("B1").setValue("lastname");
    sheet.getRange("C1").setValue("private email");
    sheet.getRange("D1").setValue(groupName + " email");
    sheet.getRange("E1").setValue("phone");
    sheet.getRange("F1").setValue("mobile");
    sheet.getRange("G1").setValue("street");
    sheet.getRange("H1").setValue("zip");
    sheet.getRange("I1").setValue("city");
    sheet.getRange("J1").setValue("group");
    sheet.getRange("K1").setValue("date1");
    sheet.getRange("L1").setValue("date2");
    sheet.getRange("M1").setValue("date3");
    sheet.getRange("N1").setValue("date4");
  
    for ( var i = 0; i &lt; contacts.length; i++) {
      var addressArray = new Array();
      addressArray = contacts[i].getAddresses(ContactsApp.Field.HOME_ADDRESS);
      var addressArrayLength = addressArray.length;    
      for ( var ii = 0; ii &lt; addressArrayLength; ii++ ) {
        var address = addressArray[ii].getAddress();
        var addressSplit = address.split(/\r\n|\r|\n/);
        var street = addressSplit[0];
        var city = addressSplit[1].split(" ")[0];
        var zip = addressSplit[1].split(" ")[1];
      }
      var homePhone = contacts[i].getHomePhone().replace("0049-","0").toString();
      var mobilePhone = contacts[i].getMobilePhone().replace("0049-","0").toString();
      var fireMail = ""
      var privateMail = "";
      var mailArray = new Array();
      mailArray = contacts[i].getEmails(ContactsApp.Field.HOME_EMAIL);
      var mailArrayLength = mailArray.length;    
      for ( var ii = 0; ii &lt; mailArrayLength; ii++ ) {
        privateMail = contacts[i].getEmails(ContactsApp.Field.HOME_EMAIL)[ii].getAddress();
      }
  
      if (!privateMail)
      {
        mailArray = contacts[i].getEmails(ContactsApp.Field.WORK_EMAIL);
        mailArrayLength = mailArray.length;
  
        for ( var ii = 0; ii &lt; mailArrayLength; ii++ ) {
          privateMail = contacts[i].getEmails(ContactsApp.Field.WORK_EMAIL)[ii].getAddress();
        }
      }
  
      mailArray = contacts[i].getEmails(groupName);
      mailArrayLength = mailArray.length;
  
      for ( var ii = 0; ii &lt; mailArrayLength; ii++ ) {
        fireMail = contacts[i].getEmails(groupName)[ii].getAddress();
      }
  
      sheet.getRange(1*i+2, 1, 1, 1).setValue(contacts[i].getGivenName());
      sheet.getRange(1*i+2, 2, 1, 1).setValue(contacts[i].getFamilyName());
      sheet.getRange(1*i+2, 3, 1, 1).setValue(privateMail);
      sheet.getRange(1*i+2, 4, 1, 1).setValue(fireMail);
      sheet.getRange(1*i+2, 5, 1, 1).setValue(homePhone);
      sheet.getRange(1*i+2, 6, 1, 1).setValue(mobilePhone);
      sheet.getRange(1*i+2, 7, 1, 1).setValue(street);
      sheet.getRange(1*i+2, 8, 1, 1).setValue(zip);
      sheet.getRange(1*i+2, 9, 1, 1).setValue(city);
  
      var groups = contacts[i].getContactGroups();
  
      for ( var j = 0; j &lt; groups.length; j++) {
        var name = groups[j].getName();
        switch (name) {
          case "group1":
            sheet.getRange(1*i+2, 10, 1, 1).setValue(1);
            break;
          case "group2":
            sheet.getRange(1*i+2, 10, 1, 1).setValue(2);
            break;
          case "group3":
            sheet.getRange(1*i+2, 10, 1, 1).setValue(3);
            break;
          case "group4":
            sheet.getRange(1*i+2, 10, 1, 1).setValue(4);
            break;
          case "group5":
            sheet.getRange(1*i+2, 10, 1, 1).setValue(5);
            break;
          default:
            break;
        }
  
        if (name.indexOf("datetrigger1") &gt; -1)
        {
          var dateArray = contacts[i].getDates("date1");
          var dateArrayLength = dateArray.length;        
          for ( var ii = 0; ii &lt; dateArrayLength; ii++ ) {
            var date = contacts[i].getDates("date1")[ii];
            var day = date.getDay();
            var month = date.getMonth();
            var year = date.getYear();
            sheet.getRange(1*i+2, 11, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
          }
          sheet.getRange(1*i+2, 11, 1, 1).setBackgroundColor('green');
        }
  
        if (name.indexOf("datetrigger2") &gt; -1)
        {
          var dateArray = contacts[i].getDates("date2");
          var dateArrayLength = dateArray.length;        
          for ( var ii = 0; ii &lt; dateArrayLength; ii++ ) {
            var date = contacts[i].getDates("date2")[ii];
            var day = date.getDay();
            var month = date.getMonth();
            var year = date.getYear();
            sheet.getRange(1*i+2, 12, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
          }
          sheet.getRange(1*i+2, 12, 1, 1).setBackgroundColor('green');
        }
  
        if (name.indexOf("datetrigger3") &gt; -1)
        {
          var dateArray = contacts[i].getDates("date3");
          var dateArrayLength = dateArray.length;        
          for ( var ii = 0; ii &lt; dateArrayLength; ii++ ) {
            var date = contacts[i].getDates("date3")[ii];
            var day = date.getDay();
            var month = date.getMonth();
            var year = date.getYear();
            sheet.getRange(1*i+2, 13, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
          }
          sheet.getRange(1*i+2, 13, 1, 1).setBackgroundColor('green');
        }
  
        if (name.indexOf("datetrigger4") &gt; -1)
        {
          var dateArray = contacts[i].getDates("date4");
          var dateArrayLength = dateArray.length;
          if (dateArrayLength &gt; 0) {
            for ( var ii = 0; ii &lt; dateArrayLength; ii++ ) {
              var date = contacts[i].getDates("date4")[ii];
              var day = date.getDay();
              var month = date.getMonth();
              var year = date.getYear();
              sheet.getRange(1*i+2, 14, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
            }
            sheet.getRange(1*i+2, 14, 1, 1).setBackgroundColor('green');
          }
        }
      }
    }
  
    Browser.msgBox(contacts.length + " contacts imported.");
  }
  </code></pre>
  
  <p>Works fine, but after the message "xy contacts imported" I get this error message:</p>
  
  <blockquote>
    <p>Unexpected exception upon serializing continuation</p>
  </blockquote>
  
  <p>I cannot figure out what's wrong.</p>
  
  <p><strong>UPDATE 2012-07-26:</strong></p>
  
  <p>I have an additional error:</p>
  
  <blockquote>
    <p>Dienst-Fehler: ContactsApp: Temporary problem - please try again later
    and consider using batch operations. The user is over quota. (Zeile
    96)</p>
  </blockquote>
  
  <p>It's about this row: <code>var groups = contacts[i].getContactGroups();</code></p>
  
  <p>I already tried it again later ... error still comes. Always I get results in my list. The error comes after various iterations of the loop.</p>
  
  <p><strong>UPDATE 2012-07-27:</strong></p>
  
  <p>I added <code>Utilities.sleep(2000);</code> after <code>var groups = contacts[i].getContactGroups();</code>. This is already sufficient. There are not as many contacts ...</p>