I am trying to delete a custom field based on its value, but it dies in testing. Current code:
function testDelete2() {
var contacts = ContactsApp.findContactGroup('test').getContacts();
for (var i in contacts) {
var checkfield = contacts[i].getUserDefinedField('testlabel')
if (checkfield == 'testvalue') {
var customFields = contacts[i].getCustomFields();
customFields.deleteCustomField('testlabel');
}
}
}
I get this error: TypeError: Cannot find function deleteCustomField in object CustomField.
No idea what that means. Please help. I've read this page over and over and it's no help: https://developers.google.com/apps-script/service_contacts
I even tried this variation which didn't work:
customFields.getLabel('testlabel').deleteCustomField();
Also, is there any simple documentation with samples anywhere of how to deal with google contacts custom fields? Adding, deleting, just getting the value all seem impossible. I appreciate the help with this question, but also don't mind finding a guide somewhere with simple samples to look at.
Using Serge's great code as inspiration came up with this code for deletion (will add full code with delete/add soon):
UPDATE: simplified process (don't know why didn't try this out in beginning, maybe I did but was coding wrong, anyway) by taking out the delete/add custom field and just updating that custom field's value
function testUpdateDues() {
var duescos = ContactsApp.findContactGroup('z8 - Assoc').getContacts();
for (var i in duescos) {
var customFields = duescos[i].getCustomFields();
for (var n in customFields) {
if (customFields[n].getLabel() == 'Dues Amount' && customFields[n].getValue() == 'unstated'){
customFields[n].setValue('$ 500');
}
}
}
}
Final Edit allows me to add/edit any custom field based on google contact group assignment (thanks Serge for the assist!!) with time based triggers in the script:
function UpdateRegion1() {
UpdateCustomField('Reg 1 - Pan', 'Region' , 'Region 1 - Panhandle');
}
function UpdateCustomField(group, customlabel, customvalue) {
var contacts = ContactsApp.findContactGroup(group).getContacts();
for (var i in contacts) {
var fields = new Array();
var customFields = contacts[i].getCustomFields();
for(var n in customFields) {
fields.push(customFields[n].getLabel());
}
if (fields.indexOf(customlabel)==-1){
contacts[i].addCustomField(customlabel, customvalue);
}
for(var j in customFields) {
if (customFields[j].getLabel() == customlabel && customFields[j].getValue() != customvalue){
customFields[j].setValue(customvalue);
}
}
}
}
Utilities.sleep(4000)
? did you try without it ?