Search code examples
google-apps-scriptgoogle-people-api

Google Apps Script - People API - How to update a contact (add them to a new contactGroup)?


I want to take a spreadsheet of e-mails names and either A) add them as new contacts or B) update existing contacts to a specific Group/Label in my contacts list. I got the "add" part down, and I'm able to retrieve things like the Person ID and such. But for updating people already existing in my contacts, I'm getting stuck.

I have a simple function (based on Google's documentation) to update a contact by Person ID and Label/Group ID. Whenever I execute it, it throws this error:

Exception: Invalid number of arguments provided. Expected 2-3 only

I've tried to follow the Google People API documentation, but it's a bit difficult for me since the examples are in JavaScript and I'm trying to use this in an Apps Script (and I'm no pro at either).

My main code is below for reference, where personID is a string that follows the people/IDNUMBER format, and groupLabel is a string in the contactGroups/GROUPIDNUMBER format:

function addContactToGroup(personID, groupLabel) {
    Logger.log("Updating: " + personID + " --> " + groupLabel);
    return People.People.updateContact({
      "resourceName": personID,
      "personFields": "memberships",
      "updatePersonFields": "memberships",
      "resource": {
        "memberships": [
          {
            "contactGroupMembership": {
              "contactGroupResourceName": groupLabel
            }
          }
        ]
      }
    });
}

EDITED CODE:

Answering my own question (sort of) because I did a bit of digging and found this thread.

But now I'm having issues with "etag." Is there a specific way I can get the etag to pass into the function? My updated (semi-working) code is:

function modifyContact(person, label) {
    Logger.log("Updating: " + person + " --> " + label);
    return People.People.updateContact({
        "resourceName": person,
        "etag": etag,
        "memberships": [
            {
            "contactGroupMembership": {
            "contactGroupResourceName": label
            }
          }
        ]
    },
        person,
        {updatePersonFields: "memberships"});
}

Solution

  • I believe your goal is as follows.

    • You want to work your function modifyContact using the valid value of etag.

    In this case, how about retrieving etag using Method: people.get? When this is reflected in your script, it becomes as follows.

    Modified script:

    function modifyContact(person, label) {
      const { etag } = People.People.get(person, { personFields: "memberships" });
      return People.People.updateContact({
        "resourceName": person,
        "etag": etag,
        "memberships": [
          {
            "contactGroupMembership": {
              "contactGroupResourceName": label
            }
          }
        ]
      },
        person,
        { updatePersonFields: "memberships" });
    }
    
    • When this script is run with the valid values of person, label, the label of person is changed to label.

    Note:

    • In this modification, it supposes that your values of person, label are valid values, and also, you have permission for updating it. Please be careful about this.

    Reference: