I have created a script that reads contact's info from a sheet and create a new contact in Google contacts. It works fine and end by saving in a second sheet the external contact ID, Resource name and etag. The idea is to reused this lookup information to find contacts that might not have an email or a phone number at first. (They will always at least have a first name and a physical address.)
ID Personne Appsheet Ressource name etag
CF32F4F6 people/c184383266205945415 %EigBAgMEBQYHCAkKCwwNDg8QERITFBUWFxkfISIjJCUmJy40NTc9Pj9AGgQBAgUHIgxMNlNjOFJMcG55TT0=
When I try to update a contact's info the updateContact function fails and I am to rookie at this to find the obvious.
It seems like this line fails: const contact = People.People.getContact(resourceName);
function updateContact(personData) {
const spreadsheetId = "1NNkW8joHAWXgatSXyAu0O4fjPgkgYeZVjx4s9KZkAZw"; // Replace with your spreadsheet ID
const ss = SpreadsheetApp.openById(spreadsheetId);
const lookupSheet = ss.getSheetByName("Mapping Google Contacts");
let resourceName = findContactByAppsheetId(personData.appsheetId, lookupSheet);
Logger.log("resourceName : " + resourceName)
Logger.log("resourceName (before prefix): " + resourceName);
if (resourceName) {
try {
Logger.log("getcontact");
const contact = People.People.getContact(resourceName);
Logger.log("getcontact after " + contact);
if (contact) {
Logger.log("Contact found");
const updatedContact = { // Create a *new* contact object
names: [{
givenName: personData.firstName || (contact.names && contact.names.length > 0 ? contact.names[0].givenName : ""),
familyName: personData.lastName || (contact.names && contact.names.length > 0 ? contact.names[0].familyName : "")
}],
phoneNumbers: [], // Initialize phoneNumbers as an empty array
emailAddresses: [], // Initialize emailAddresses as an empty array
organizations: [], // Initialize organizations as an empty array
addresses: [], // Initialize addresses as an empty array
birthdays: contact.birthdays ? [...contact.birthdays] : [],
user_defined: contact.user_defined ? {...contact.user_defined} : {}
};
Logger.log("updatedContact created");
Any help on how I can fix this will be appreciated.
I have created a script that reads contact's info from a sheet and create a new contact in Google contacts. It works fine and end by saving in a second sheet the external contact ID, Resource name and etag. The idea is to reused this lookup information to find contacts that might not have an email or a phone number at first. (They will always at least have a first name and a physical address.)
ID Personne Appsheet Ressource name etag
CF32F4F6 people/c184383266205945415 %EigBAgMEBQYHCAkKCwwNDg8QERITFBUWFxkfISIjJCUmJy40NTc9Pj9AGgQBAgUHIgxMNlNjOFJMcG55TT0=
When I try to update a contact's info the updateContact function fails and I am to rookie at this to find the obvious.
It seems like this line fails: const contact = People.People.getContact(resourceName);
function updateContact(personData) {
const spreadsheetId = "1NNkW8joHAWXgatSXyAu0O4fjPgkgYeZVjx4s9KZkAZw"; // Replace with your spreadsheet ID
const ss = SpreadsheetApp.openById(spreadsheetId);
const lookupSheet = ss.getSheetByName("Mapping Google Contacts");
let resourceName = findContactByAppsheetId(personData.appsheetId, lookupSheet);
Logger.log("resourceName : " + resourceName)
Logger.log("resourceName (before prefix): " + resourceName);
if (resourceName) {
try {
Logger.log("getcontact");
const contact = People.People.getContact(resourceName);
Logger.log("getcontact after " + contact);
if (contact) {
Logger.log("Contact found");
const updatedContact = { // Create a *new* contact object
names: [{
givenName: personData.firstName || (contact.names && contact.names.length > 0 ? contact.names[0].givenName : ""),
familyName: personData.lastName || (contact.names && contact.names.length > 0 ? contact.names[0].familyName : "")
}],
phoneNumbers: [], // Initialize phoneNumbers as an empty array
emailAddresses: [], // Initialize emailAddresses as an empty array
organizations: [], // Initialize organizations as an empty array
addresses: [], // Initialize addresses as an empty array
birthdays: contact.birthdays ? [...contact.birthdays] : [],
user_defined: contact.user_defined ? {...contact.user_defined} : {}
};
Logger.log("updatedContact created");
Any help on how I can fix this will be appreciated.
The issue is that you're using People.People.getContact()
, which does not exist. Instead, you should use People.People.get()
to retrieve a contact.
So try this:
const contact = People.People.get(resourceName, {
personFields: 'names,emailAddresses,phoneNumbers,addresses,organizations,birthdays,user_defined'
});
Instead of:
const contact = People.People.getContact(resourceName);
I was also missing this line in the updatedContact declaration in order for the People.People.updateContact() to work.
etag: contact.etag
phoneNumbers: [], // Initialize phoneNumbers as an empty array
emailAddresses: [], // Initialize emailAddresses as an empty array
organizations: [], // Initialize organizations as an empty array
addresses: [], // Initialize addresses as an empty array
birthdays: contact.birthdays ? [...contact.birthdays] : [],
etag: contact.etag