function matchAndReplace(){
var oldDomain = "@olddomain.com";
var newDomain = "@newdomain.com";
// retrieve all the user's contacts
var contacts = ContactsApp.getContacts();
Logger.log('num '+contacts.length);
for (var i = 0; i < contacts.length; i++) {
var emails = contacts[i].getEmails();
Logger.log(emails.length+' emails');
for (var e in emails) {
var email = emails[e].getAddress();
//emails[e].setLabel(ContactsApp.Field.WORK_EMAIL);
Logger.log(email);
if (email.indexOf(oldDomain) !== -1) {
//remove
emails[e].deleteEmailField(); <-- error
Logger.log(email+' removed');
//add
var newEmail = email.split("@")[0]+newDomain;
var newEmailField = contacts[i].addEmail(ContactsApp.Field.WORK_EMAIL, newEmail);
Logger.log(newEmail+' added');
}
}
//break;
}
}
Error:
Service error: ContactsApp: Entry does not have any fields set. (line 20, file "Code")