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?
In your situation, how about modifying as follows?
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" });
}
label
is added to the contact.