javascriptgoogle-apps-scriptgoogle-people-api

Using the externalDs field for a Person in People API, working in Google Apps Script


This is probably a dumb question, and if so I apologize. I have searched here and on Google and can't find an answer.

I want to be able to modify the externalIds field for a Person. I'm able to modify other fields like names. Generally like:

let query = "John Doe";
let found = People.People.searchContacts({
  "query": query,
  "readMask": "names"
});
let thisResult = found.results[0];
thisResult.person.names[0].familyName = "Smith";
People.People.updateContact(thisResult.person, thisResult.person.resourceName, {updatePersonFields: "names"});

I can't figure out how this works with externalIds though. Trying to modify thisResult.person.externalIDs[0] results in undefined. Also, I'm not clear what is meant by predefined types. Isn't type just another string field within an externalId object, like familyName is in a name object? What would prevent multiple externalIds from having the same type?

Anyway, after much frustration I've been able to find no documentation on this, so any help would be much appreciated.


Solution

  • This normally happens when the person object doesn't contain any values under externalIds you can try this modification that will add the specified externalId object to the contact:

    function myFunction() {
      let query = "John Doe";
      let found = People.People.searchContacts({
        "query": query,
        "readMask": "externalIds"
      });
      let thisResult = found.results[0];
      var externalId = { value: "AN_ID",type: "account" };
      var resourceToUpdate = {
        externalIds: externalId,
        etag: thisResult.person.etag
      };
      People.People.updateContact(resourceToUpdate, thisResult.person.resourceName, { updatePersonFields: "externalIds" });
    }
    

    Or you could use this version to just add a new externalId instead of overwriting them:

    function myFunction() {
      let query = "John Doe";
      let found = People.People.searchContacts({
        "query": query,
        "readMask": "externalIds"
      });
      let thisResult = found.results[0];
      var externalId = { value: "AN_ID", type: "account" };
    
      if (thisResult.person.externalIds) {
        thisResult.person.externalIds.push(externalId);
        var resourceToUpdate = thisResult.person;
    
      } else {
        var resourceToUpdate = {
          externalIds: externalId,
          etag: thisResult.person.etag
        };
      }
    
      People.People.updateContact(resourceToUpdate, thisResult.person.resourceName, { updatePersonFields: "externalIds" });
    }
    

    Note that:

    enter image description here