Cooper
Cooper

Reputation: 64130

Can you explain why I'm getting this error: Unexpected error while getting the method or property getContacts on object ContactsApp

This is the Error:

Exception: Unexpected error while getting the method or property getContacts on object ContactsApp. (line 2, file "ag1")

The Original Question:

The code came from here. It's a previous answer to a question. I don't understand how I can be getting that error there.

function getAllContacts() {
  var contactsA=ContactsApp.getContacts();//The Error occurs on this line
  var s='';
  var br='<br />';//line delimiter change to linefeed when not using html dialog
  var dlm=' ~~~ ';//field delimiter
  for(var i=0;i<contactsA.length;i++) {
    s+=Utilities.formatString('<br />\'%s\',\'%s\',\'%s\',\'%s\',\'%s\'%s',
    (typeof(contactsA[i].getFullName())!='undefined')?contactsA[i].getFullName():'',
    (typeof(contactsA[i].getAddresses().map(function (v,i,A) { return A[i].getAddress();}))!='undefined')?contactsA[i].getAddresses().map(function (v,i,A) { return A[i].getAddress();}).join(dlm):'',
    (typeof(contactsA[i].getEmails().map(function(v,i,A) {return A[i].getAddress();}))!='undefined')?contactsA[i].getEmails().map(function(cV,i,A) {return A[i].getAddress();}).join(dlm):'',
    (typeof(contactsA[i].getPhones().map(function(v,i,A){return A[i].getPhoneNumber();}))!='undefined')?contactsA[i].getPhones().map(function(v,i,A){return A[i].getPhoneNumber();}).join(dlm):'',
    (typeof(contactsA[i].getCompanies().map(function(v,i,A){return A[i].getCompanyName();}))!='undefined')?contactsA[i].getCompanies().map(function(v,i,A){return A[i].getCompanyName();}).join(dlm):'',br);
  }
  var ui=HtmlService.createHtmlOutput(s).setWidth(800)  ;
  SpreadsheetApp.getUi().showModelessDialog(ui, 'Contacts')
}

Actually, I can run the code in another spreadsheet with no errors but in the spreadsheet that I use all of the time with StackOverflow Questions it fails.

This function fails on line 2 also:

function testing101() {
  const cA=ContactsApp.getContacts();
  Logger.log(cA.length);
}

After Fix For UrlFetch Issue Roll Out:

Still getting the same error in this file. It still works in other files. Here's my manifest file (minus my libraries):

{
  "timeZone": "America/Denver",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Drive",
      "serviceId": "drive",
      "version": "v2"
    }, {
      "userSymbol": "People",
      "serviceId": "peopleapi",
      "version": "v1"
    }, {
      "userSymbol": "Slides",
      "serviceId": "slides",
      "version": "v1"
    }, {
      "userSymbol": "Gmail",
      "serviceId": "gmail",
      "version": "v1"
    }, {
      "userSymbol": "Sheets",
      "serviceId": "sheets",
      "version": "v4"
    }, {
      "userSymbol": "DriveActivity",
      "serviceId": "driveactivity",
      "version": "v2"
    }, {
      "userSymbol": "Calendar",
      "serviceId": "calendar",
      "version": "v3"
    }],
  "webapp": {
    "access": "MYSELF",
    "executeAs": "USER_DEPLOYING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://mail.google.com/", "https://www.google.com/m8/feeds", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/script.container.ui", "https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/script.send_mail", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/presentations", "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/documents", "https://www.googleapis.com/auth/script.projects.readonly", "https://sites.google.com/feeds", "https://www.googleapis.com/auth/drive.activity"],
  "runtimeVersion": "V8"
}

Upvotes: 0

Views: 537

Answers (2)

Iamblichus
Iamblichus

Reputation: 19339

You need to enable Contacts API in the GCP project.

Based on your comment in Issue Tracker:

It's a cloud platform project.

I think the issue here is that you haven't enabled Contacts API in the GCP project. Once you do that, this problem should go away.

Therefore, I don't think this problem is related to the UrlFetch issue.

Update - Regarding standard and advanced services:

@TheMaster It's not just advanced services that require enabling the corresponding API in the GCP project; some of the standard services, including the current one or, for example, DriveApp.getRootFolder(), require that. See this issue:

Specifically this comment:

There are mentions about enabling APIs for advanced services here. But not for the standard services, I notified this to the documentation team.

At this point it's still unclear whether this is a problem of the documentation, which should mention not only Advanced Services, or if it's a bug related to these failing standard services.

In any case, this has been reported internally in Issue Tracker and it's still under investigation. And the issue the OP is having can be fixed by enabling Contacts API in the GCP project.

Reference:

Upvotes: 3

Cooper
Cooper

Reputation: 64130

According to TheMaster this is probably related to this issue Evidently, getContacts() utilizes UrlFetchApp under the hood. So if you're having this sort of problem please go to the issue and star it to let them know that you're having the problem too. The more people that have the problem I guess the more likely it will get fixed quickly.

Having said that I'll leave the question unchecked on the outside chance that someone else can provide an even better answer.

lamblichuus reports that the UrlFetch issue rollout has occurred but I'm still seeing the same error so I'm guessing this may not be the answer.

Upvotes: 1

Related Questions