How can I add a label to a contact while creating it with Google Apps Script?

Viewed 550

I can create a new Google contact on submission of a Form. I need to add a label to these contacts but I couldn't find any method to do that. Is it possible to add a label? Here is a snippet of my code for finding/creating a contact:

var contact = ContactsApp.getContact(email);
if(!contact){
  contact = ContactsApp.createContact(firstName, lastName, email);
}
1 Answers

To add label to a contact, add Contact to a ContactGroup. Here is a sample code:

  let labelName = 'myLabel';
  let firstName = 'John';
  let lastName = 'Doe';
  let email = 'john.doe@gmail.com';

  //find or create the Contact
  let contact = ContactsApp.getContact(email) || ContactsApp.createContact(firstName, lastName, email);
  }

  //find the Label
  let group = ContactsApp.getContactGroup(labelName);

  //add label to the contact
  group.addContact(contact);
Related