title: delete custom field google apps script
body: <p>I am trying to delete a custom field based on its value, but it dies in testing. Current code:</p>
  
  <pre><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');
          }
      }
  }
  </code></pre>
  
  <p>I get this error: TypeError: Cannot find function deleteCustomField in object CustomField.</p>
  
  <p>No idea what that means. Please help. I've read this page over and over and it's no help: <a href="https://developers.google.com/apps-script/service_contacts" rel="nofollow">https://developers.google.com/apps-script/service_contacts</a></p>
  
  <p>I even tried this variation which didn't work:</p>
  
  <pre><code>      customFields.getLabel('testlabel').deleteCustomField();
  </code></pre>
  
  <p>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.</p>
  
  <p>Using Serge's great code as inspiration came up with this code for deletion (will add full code with delete/add soon):</p>
  
  <p>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</p>
  
  <pre><code>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' &amp;&amp; customFields[n].getValue() == 'unstated'){
  customFields[n].setValue('$ 500');
  }
  }
  }
  }
  </code></pre>
  
  <p><strong>Final Edit</strong> 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:</p>
  
  <pre><code>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 &amp;&amp; customFields[j].getValue() != customvalue){
          customFields[j].setValue(customvalue);
        }
      }
    }
  }
  </code></pre>