79418993

Date: 2025-02-06 18:32:58
Score: 5
Natty:
Report link

Thanks @Zeros-N-Ones!

This works to find the record to update. My next issue is the updateContact() (at the bottom), which fails. Any additional help will be greatly appreciated.

      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] : []
    };
    Logger.log("updatedContact created");

    // Update other fields - phone numbers, email, organizations, addresses, and birthdays
    if (personData.homePhone) {
      updatedContact.phoneNumbers.push({ value: personData.homePhone, type: "Home" });
    }
    if (personData.mobilePhone) {
      updatedContact.phoneNumbers.push({ value: personData.mobilePhone, type: "Mobile" });
    }

    if (personData.email) {
      updatedContact.emailAddresses.push({ value: personData.email, type: "Personel" });
    }

    if (personData.company) {
      updatedContact.organizations.push({ name: personData.company });
    }

    if (personData.address) {
      updatedContact.addresses.push({ formattedValue: personData.address });
    }

    if (personData.birthdate) {
      try {
        const parsedDate = parseDate(personData.birthdate);
        if (parsedDate) {
          const birthday = People.newBirthday();
          const date = People.newDate();

          date.year = parsedDate.year || null;
          date.month = parsedDate.month || null;
          date.day = parsedDate.day || null;

          birthday.date = date;
          updatedContact.birthdays = [birthday];
        } else {
          Logger.log("Warning: Invalid birthdate format: " + personData.birthdate);
        }
      } catch (error) {
        Logger.log("Error setting birthdate: " + error);
        Logger.log("Error Details: " + JSON.stringify(error));
      }
    }
    Logger.log("Contact object BEFORE update: " + JSON.stringify(updatedContact, null, 2));

    var updatePersonFields = "updatePersonFields=names,emailAddresses,phoneNumbers,addresses,organizations,birthdays";
    const finalContact = People.People.updateContact(updatedContact, resourceName, {updatePersonFields: "names,emailAddresses,phoneNumbers,addresses,organizations,birthdays"});
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Blacklisted phrase (1): appreciated
  • RegEx Blacklisted phrase (3): help will be greatly appreciated
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Zeros-N-Ones
  • Low reputation (1):
Posted by: shimoda