Reputation: 11
I am new to coding and am attempting to use information in a Google Sheet to create a Google Contact. The code works, but I always get an error. Any help will be greatly appreciated.
Here is my code:
function newContact () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Contacts");
var data = sheet.getDataRange().getValues();
for (var i = 1; i < data.length; i++) {
var lastName = data[i][0];
var firstName = data[i][1];
var email = data[i][12];
const obj = {
contacts: [
{
contactPerson: {
emailAddresses: [{ value: email }],
names: [
{ familyName: lastName, givenName: firstName },
],
},
},
,
,
,
],
readMask: "emailAddresses,names",
};
const res = People.People.batchCreateContacts(obj);
console.log(res);
}
}
Whenever I run the code, everything works, except I get this error:
Error
GoogleJsonResponseException: API call to people.people.batchCreateContacts failed with error: Must specify a non-metadata contact field on the person. newContact @ Code.gs:29
Line 29 is: const res = People.People.batchCreateContacts(obj);
Upvotes: 1
Views: 98
Reputation: 654
It's possible that @Lime Husky's comment is causing the issue:
Is it possible that you only forgot to add the People API Service
If this might be the case, see Enable advanced services.
I tested your code, and it works well after I added the People API service.
Execution log:
2:27:48 AM Notice Execution started
2:27:50 AM Info { createdPeople:
[ { status: {},
requestedResourceName: 'people/c85598145986373786**',
person: [Object],
httpStatusCode: 200 } ] }
2:27:52 AM Info { createdPeople:
[ { person: [Object],
httpStatusCode: 200,
status: {},
requestedResourceName: 'people/c58552667022245245**' } ] }
2:27:53 AM Notice Execution completed
Upvotes: 1