I am working on a Google Apps Script project that integrates with the Google People API. My goal is to update a field designated as primary (e.g., primary address). However, I haven’t found a way to directly change the primary field by modifying the metadata of the array’s entries.
For example, adding ‘primary’: true to the metadata for the new primary entry and removing it from other entries does not override the existing primary designation.
Current Approach:
The only method I’ve found to achieve this is as follows:
Remove all entries in the array except the one that should be primary.
Update the contact.
Add back the other entries, removing their metadata to avoid conflicts.
Here’s a snippet of the function where I apply the changes, including handling the primary address update (there are quite a few class objects that I have created that perform operations in the api but aren’t part of the API so apologies if that confuses the matter but it’s the methodology I’m concerned about since this seems like an opportunity for disaster, so I don’t want to build a backup in the event of an error while information isn’t saved unless I have to):
_applyChanges(person, changes) {
if (!person || !Array.isArray(changes)) {
throw new Error('Valid contact and changes array required');
}
try {
let primaryAddressChanges = [];
let removals = [];
changes.forEach(change => {
if (change.apiFieldInfo?.arrayField === 'addresses' && change.isPrimary) {
primaryAddressChanges.push(change);
}
});
if (person.addresses && primaryAddressChanges.length > 0 && person.addresses.length > 1) {
let primaryAddress = {};
let otherAddresses = [];
person.addresses.forEach(address => {
if (address.metadata?.primary) {
primaryAddress = address;
} else {
otherAddresses.push(address);
}
});
if (primaryAddress) {
// Reorder and update addresses to make the desired one primary
let finalAddressArray = [primaryAddress].concat(otherAddresses);
let googlePerson = new GooglePerson(person);
try {
googlePerson.updateAddresses([primaryAddress]); // Only update the primary address
googlePerson.update();
googlePerson.updateAddresses(finalAddressArray); // Add back the other addresses
person = googlePerson.person;
} catch (error) {
Logger.log(`Error updating primary address: ${error.message}`);
}
}
}
return person;
} catch (error) {
Logger.log(`Error applying changes: ${error.message}`);
throw error;
}
}
Key Problem:
Why doesn’t directly adding ‘primary’: true to the metadata of the new primary entry and removing it from the others work? Is there a better or more direct approach to achieve this?
Additional Information:
The updateAddresses method in the code above is a utility method to modify the addresses of a GooglePerson instance.
The process involves updating the primary address and then re-adding the other addresses after clearing their metadata.
I’d appreciate any insights or alternative approaches. Am I missing something in the API documentation or functionality?
If you want me to refine this further, let me know!