0

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);
      }
    }
  }
}
3
  • why did you add Utilities.sleep(4000) ? did you try without it ? Nov 2, 2012 at 10:09
  • yeah, I got that etag error when a contact is edited "too quickly" - since a field is being deleted then another added in a single contact I added it to allow the delete to "take". every script where I've edited a single contact with consecutive actions has sent me the etag error if I didn't include the 4 second delay - hate to do it since it slows it down, but Ive searched and searched and it seems to be only solution that works Nov 2, 2012 at 12:47
  • fyi - i'd vote your answer a plus one, but the system still wont allow me since i'm new Nov 2, 2012 at 12:48

1 Answer 1

0

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')
      }
    }
  }
13
  • thanks - I was heading near your direction but you got me much closer faster! Thing is, I am trying to test the value not the label - does this code (I'll try it in the meantime) basically work if I swithc out getLabel with getValue? - the getUserDefinedField easily gives the value if I provide the label which is why I had it at the top to get the ball rolling by comparing it to 'testvalue'. Nov 2, 2012 at 0:17
  • and yes, I have discovered the logger.logs - very helpful thx. basically I am trying to test the value of the label of the custom field and if the test comes up true, then delete the whole field. I kind of chetead using the deprecated getUserDefinedField to get the value up top. :-) yes, sometimes I wanna punt on google, but if I can customize it then I'll be very happy for my purposes. Basic reporting and such I've discovered I can do with GAS. Nov 2, 2012 at 0:23
  • You can indeed switch the getLabel() statement with getValue() and make the comparison with that instead... sorry for the error in my code, I wasn't sure what you needed :-) I updated the code Nov 2, 2012 at 0:55
  • no you're good - learned something new - I thought the getValue was dependent on the getLabel - but they are essentially independent - so that's valuable to know. I am going to do a hybrid of yours, my current, and my original, and if it works i'll post it here - thx Nov 2, 2012 at 1:00
  • having trouble - the idea is for this function to check all the contacts in a group - and delete those custom fields with that label and that value (the 'unstated' value is a dummy value I imported into google contacts to create the custom field). the current set of code is deleting all custom fields of that label - ugh it's too difficult to explain in typing and I've been working on this one issue for 12 hours - I'm just gonna abandon this and let google claim the victory again Nov 2, 2012 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.