1

I asked another question about this particular piece of code here, but I was able to resolve that issue thanks to the help I got on that thread.

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

Now that the code is fully functional, I'm wondering if there's any way to update the contact by simply adding a label, rather than overwriting all existing labels on the contact.

If there's no easy way to do this, I'm thinking that the best alternative would be to retrieve the existing labels using people.get() (which is how I'm retrieving all other information about the person) and then pass ALL labels into people.updateContact() so that the old and new labels, combined, will overwrite the new labels.

For example, if the contact has an existing labelA, after retrieving A and defining labelB, I would simply pass them both in under "memberships" when I update the contact, so that it overwrites the memberships but retains both labels.

"memberships": [
        {
            "contactGroupMembership": {
                "contactGroupResourceName": labelA
            }
        },
                {
            "contactGroupMembership": {
                "contactGroupResourceName": labelB
            }
        }

Is this the best way to do this? Or is there a better/more preferred way of updating without overwriting existing labels?

1 Answers1

1

In your situation, how about modifying as follows?

Modified script:

function modifyContact(person, label) {
  const { etag, memberships } = People.People.get(person, { personFields: "memberships" });
  return People.People.updateContact({
    "resourceName": person,
    "etag": etag,
    "memberships": [
      ...memberships, // Here, the existing labels are included.
      { "contactGroupMembership": { "contactGroupResourceName": label } }
    ]
  },
    person,
    { updatePersonFields: "memberships" });
}
  • By this modification, label is added to the contact.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I think I understand... In this modification, because you are setting constants using the `people.get()` method, you can simply insert the constant `memberships` in the `people.updateContact()` method and it will automatically populate the existing labels? – Corinne Jacobo Aug 01 '23 at 06:02
  • 1
    @Corinne Jacobo `memberships` of `const { etag, memberships } = People.People.get(person, { personFields: "memberships" });` is the current memberships. In this answer, I include both the current memberships and your expected membership. If I misunderstood your reply, I apologize. – Tanaike Aug 01 '23 at 06:06
  • This is extremely helpful, thank you! This answered my question completely. Your English is clear enough for me to understand, don't worry about it. ありがとうございました!(Thank you again!) – Corinne Jacobo Aug 01 '23 at 06:12
  • @Corinne Jacobo Thank you for replying. I'm glad your issue was resolved. Thank you, too. こちらこそありがとうございました。 – Tanaike Aug 01 '23 at 12:03