deleteContactFromList

Regular Participant

deleteContactFromList

Hi guys, I'm trying to use deleteContactFromList() but it doesn't seem to work for me.  

 

Code sample!

 

 

//$active is an array of emails of, you guessed it, active users....
foreach($active as $a_email){ //find the contact... $tmp_cntct = $cc->getContactByEmail(ACCESS_TOKEN, $a_email); if(sizeof($tmp_cntct->results) === 0){ //add this contact to the active list...this code works just fine... $Contact = new Contact(); $Contact->addEmail($a_email); $Contact->addList($ref_active); //where $ref_active is my active list from C.C. $NewContact = $cc->addContact(ACCESS_TOKEN, $Contact); } else{ //user found, remove from non_active list $ctct = $tmp_cntct->results[0]; $cc->deleteContactFromList(ACCESS_TOKEN, $ctct, $ref_nonactive); //<-- Just doesn't work for me :( Help! //$ref_nonactive is a valid constant contact list, why wouldn't this do the job? $ctct->addList($ref_active); $cc->updateContact(ACCESS_TOKEN, $ctct, false); //working as well :/ } }

 

 

Any assistance would be great

12 REPLIES 12
Member

Hello,

 

Thanks for posting in the forums.  What response are you getting back from deleteContactFromList()?

 

Andrew B.

API Support, Constant Contact

  Thanks for the reply,

 

var_dump of the response: bool(true)

 

 

How are you determining that the API call is not working?

  I log into my Constant contact account to see the email has not been removed from the list.   In this case, the user is added to my "active" list, but is not removed from my "non_active" list.  

 

  

Right now there is a delay in the UI reflecting changes made to an account using the API.  So even though you are correctly removing a contact, it will not change in the UI right away.  There is a defect logged to resolve the issue, but I don't have a time frame for its resolution right now.  The best way to check if a contact has been removed at the moment is to verify it through the API.

  Interesting....any idea how long the delay is?   I ran this code last night (about 16 hours ago)

 

 

  Can you provide a brief example on checking this via the API?

Logged me in here from our clients account after I went to check :/

 

Anyway;

 

 **Edit...have written my own check....

Confirmed, after using the API to check, the contact still belongs to the list.

 

Is it better to modify the contact->lists directly, then perform updateContact()  ?

 

 

 

 

Took a look at the SDK (based on the code, the php sdk).  The deleteContactFromList method needs the contact's id as a parameter as a string.  Can you try passing just the contact id string, instead of the entire $ctct, which looks like a Contact object?

 

Edit:  The contact id is an int, sorry.

Herm....

 

echo 'CHECKING ACTIVE MEMBERSHIPS...<p>';
foreach($active as $a_email){
  //find the contact...
  echo 'searching for cc email '.$a_email.'<br>';
  $tmp_cntct = $cc->getContactByEmail(ACCESS_TOKEN, $a_email);
  if(sizeof($tmp_cntct->results) === 0){
  echo 'user not found.  Adding '.$a_email.'<br>';
    //add this contact to the active list...
    $Contact = new Contact();
    $Contact->addEmail($a_email);
    $Contact->addList($ref_active);
    $NewContact = $cc->addContact(ACCESS_TOKEN, $Contact);
    echo 'added to active list successfully';
  }
  else{
    echo 'user '. $a_email .' found, remove from inactive list and add to active list<br>';
    $ctct = $tmp_cntct->results[0];
   if(checkInList($ctct, $ref_nonactive->id)){
     echo 'Found user '. $a_email .' in nonactive list....removing...<p>';
     $ret = $cc->deleteContactFromList(ACCESS_TOKEN, $ctct->id, $ref_nonactive);
   }
    $ctct->addList($ref_active);
    $cc->updateContact(ACCESS_TOKEN, $ctct, false);
  }
}


$chk = $cc->getContactByEmail(ACCESS_TOKEN, "blah@blah.com");
$usrchk = $chk->results[0];
echo 'Check users lists: <p>';
var_dump($usrchk->lists);

 

 

Outputs:

 

searching for cc email blah@blah.com


user blah@blah.com found, remove from inactive list, and add to active list


checking 3 == 3

 

Found user blah@blah.com in nonactive list....removing...(id: 6)

 

Check users lists (active list id: 3, non_active list id: 4):

 

array(2) { [0]=> object(Ctct\Components\Contacts\ContactList)#35 (4) { ["id"]=> string(1) "3" ["name"]=> NULL ["status"]=> string(6) "ACTIVE" ["contact_count"]=> NULL } [1]=> object(Ctct\Components\Contacts\ContactList)#40 (4) { ["id"]=> string(1) "4" ["name"]=> NULL ["status"]=> string(6) "ACTIVE" ["contact_count"]=> NULL } }

 

 

  *For good measure I also tried passing the list as an id as well.....same results :(   I feel like everything is in place, heck I even wrote a quick check to prevent it from trying to remove a user form a list they may not belong to in the first place...

This seems to do the trick for me...not using that function at all...

 

echo 'CHECKING NON-ACTIVE MEMBERSHIPS...<p>';

foreach($all_others as $na_email){
  //find the contact...
  echo "searching for cc email ".$na_email."<br>";
  $tmp_cntct      = $cc->getContactByEmail(ACCESS_TOKEN, $na_email);
  if(sizeof($tmp_cntct->results) === 0){
  echo "user not found.  Adding ".$na_email."<br>";
    //add this contact to the non active list...
    $Contact      = new Contact();
    $Contact->addEmail($na_email);
    $Contact->addList($ref_nonactive);
    $NewContact   = $cc->addContact(ACCESS_TOKEN, $Contact);
    echo 'added to nonactive list successfully';
  }
  else{
    echo "user ". $na_email ." found, remove from active list, add to non active list<br>";
    $ctct         = $tmp_cntct->results[0];
    if(checkInList($ctct, $ref_active->id)){
     // echo 'Found user '. $na_email .' in active list....removing...(id: '. $ctct->id .')<p>';
      //echo 'params: '. ACCESS_TOKEN .' '.$ctct->id .' '.$ref_active->id.'<p>';
      //$ret        = $cc->deleteContactFromList(ACCESS_TOKEN, (int)$ctct->id, $ref_active);
      removeFromList($cc, $ctct, $ref_active);
    }
    $ctct->addList($ref_nonactive);
    $cc->updateContact(ACCESS_TOKEN, $ctct, false);
  }
}

## =========================
## Function check in List
## =========================
function checkInList($cntct, $listid){
  $ret            = false;
  $lists          = $cntct->lists;
  foreach($lists as $contactlist){
    echo 'checking '.$contactlist->id .' == '. $listid .'<p>';
    if($contactlist->id === $listid){
      $ret        = true;
    }
  }
  return $ret;
}

#==============================
#Function removeFromList
#============================== function removeFromList($cc, $contact, $list){ for($i=0; $i<sizeof($contact->lists); $i++){ $tmplist = $contact->lists[$i]; if($tmplist->id == $list->id){ array_splice($contact->lists, $i, 1); } } $cc->updateContact(ACCESS_TOKEN, $contact, false); }

 

...Though I feel a bit dirty doing it.   I'd feel much better if I was using that API function properly :/  

Thanks for sharing this!  I'm going to dig into this more and see if I can figure out what is happening...

-------------------------------------------
Check out our developer portal: http://developer.constantcontact.com
Developer Portal

View API documentation, code samples, get your API key.

Visit Page