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);
}
}
}
}
Here is how it works, see comments in code
function testDelete2() {
var contacts = ContactsApp.findContactGroup('test').getContacts();
for (var i in contacts) {
var customFields = contacts[i].getCustomFields();// returns an array of custom fields
for(var n in customFields) { // iterate the custom fields
Logger.log(customFields[n].getLabel()+' = '+customFields[n].getValue()) // EDIT : just changed this line to show the label AND the value
if(customFields[n].getLabel() == 'testlabel'){ // compare the label with your criteria
customFields[n].deleteCustomField();// if true then delete
}
}
}
}
I hope this answers also (in a way) to your second request... As you can see it's all about using these functions the right way. I'd suggest you make use of the logger to helps you. It allows to see what type of variable is returned ... ( just between you and me, that's how I did 'debug' your code.... but please keep this confidential ;-)
I saw on your profile that you are "Using google apps script because I have to" but I'm sure (well I hope) you'll love it in the end.
EDIT 2 : A script to add a custom field when not present
function addCustomField() {
var contacts = ContactsApp.findContactGroup('Insas').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())
}
Logger.log(fields)
if(fields.indexOf('insas')==-1){
contacts[i].addCustomField('insas','oui')
}
}
}