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);
      }
    }
  }
}
user1783229
  • 193
  • 2
  • 3
  • 14
  • why did you add `Utilities.sleep(4000)` ? did you try without it ? – Serge insas Nov 02 '12 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 – user1783229 Nov 02 '12 at 12:47
  • fyi - i'd vote your answer a plus one, but the system still wont allow me since i'm new – user1783229 Nov 02 '12 at 12:48

1 Answers1

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')
      }
    }
  }
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • 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'. – user1783229 Nov 02 '12 at 00: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. – user1783229 Nov 02 '12 at 00: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 – Serge insas Nov 02 '12 at 00: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 – user1783229 Nov 02 '12 at 01: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 – user1783229 Nov 02 '12 at 01:35
  • thx - got it to work - almost let google win, but i'm too stubborn - just had to learn real quick what the proper syntax was for what I needed - that and you code got me there - thx! – user1783229 Nov 02 '12 at 02:15
  • struggling again - I am redoing - I took out those placeholder custom fields in google contacts. so now the dues field/label/value are gone. So I can easily add the custom field - but since i'll be running this function over and over I don't want the same custom field added to contacts that already have it. having trouble with syntax and logic order - any thoughts how to redo the above code to fit what I am doing? – user1783229 Nov 02 '12 at 17:21
  • alright, went back and added the 'unstated' and simplified the code by using setValue which I discovered works - so no longer deleting and adding - just updating - new code in place of old code above – user1783229 Nov 02 '12 at 19:23
  • that's great - thx, will work to incorporate it (to your comment before my last - which was not loaded when I made last comment fyi) – user1783229 Nov 02 '12 at 20:20
  • for some reason this was not working - it seemed checking the contact to see if the fields was present was skipping it unless the custom field was in the last spot of the indexof array. weird – user1783229 Nov 08 '12 at 16:51
  • yeah, tested again, it seems with my Final Code, the custom field will be updated only if it is the last custom field. If I take out the check for the field is present function, then the set value works fine - very weird, not sure what I am missing. – user1783229 Nov 08 '12 at 17:10
  • tested again - somehow the push method is interfering with the else if - i don't know, maybe just going to take out the add customfield and be done with this thing – user1783229 Nov 08 '12 at 17:19
  • okay, think I got it - I broke out the 2d function because it was pulling from the same 'n' and assigned it 'j' - edited code above – user1783229 Nov 08 '12 at 17:32