The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

addOrUpdate($contactInfo) - Always returns true

Highlighted
Occasional Participant

addOrUpdate($contactInfo) - Always returns true

Trying to code  section of site to update a conact and add to a 2nd list but found this function on another posts but it always returns true

if conact is already on another list and does not add to 2nd list.


public function addOrUpdate($contactInfo) {



            if (!$this->subscriberExists($contactInfo)) {

                $newContactXML = $this->createContactXML(null,$contactInfo);

                $postReturn = $this->addSubscriber($newContactXML);

                return $postReturn;

            } else {

                $currentDetails = $this->getSubscriberDetails($contactInfo);

                $contactInfo = $currentDetails;

                $updateXml = $this->createContactXML($contactInfo, $contactInfo);              

                $putReturn = $this->editSubscriber($contactInfo, $updateXml);

                return $putReturn;

                }

        }

    }

2 REPLIES 2
Highlighted
Employee

Re: addOrUpdate($contactInfo) - Always returns true

Without knowing specifically what functionality is wanted by each individual using a function to add or update, it's hard to make one that would work for everyone. The function you pasted above was meant to complete replace the entire contacts information with the details that were submitted to that function. The one below does it's best to determine what details were actually submitted to this function and need to be updated by seeing if each field actual has a value other than null/empty.


In regards to the function turning back 'true' every time, this is because the addSubscriber() and editSubscriber() functions that this function uses are always turning those values back. This is something that we will look at resolving, but I do want to let you know that we are mainly planning on maintaining our PHP Library going forward, as opposed to these older samples.


Please let me know if you have any questions or concerns regarding this. Thanks.


public function addOrUpdate($contactInfo) {

    // Add the subscriber if they do not exist

    if ($this->subscriberExists($contactInfo) == false){

        // Create XML

        $newContactXML = $this->createContactXML(null,$contactInfo);

        // Add subscriber

        $postReturn = $this->addSubscriber($newContactXML);

        $response = ($postResponse ? true: false);

    } else {

        // Get all information currently stored in Constant Contact

        $currentDetails = $this->getSubscriberDetails($contactInfo);

        foreach($contactInfo as $contactList){

            /* Check if the contact is already subscribed to any of the lists they selected in the form */

            if(!in_array($contactList, $currentDetails)){

                // Append any newly selected lists to an array of previously subscribed lists

                array_push($currentDetails, $contactList);

            }

        }

        // Update necessary fields

        foreach($contactInfo as $field => $value){

            if($field != 'lists' && !empty($value)){

                $currentDetails = $contactInfo;

            }

        }

        //create the XML using updated details as well as the existing ID

        $updateXml = $this->createContactXML($currentDetails, $currentDetails);



        //Update the subscriber using the newly created XML

        $putReturn = $this->editSubscriber($currentDetails, $updateXml);

        $response = ($putReturn ? true: false);

    }

    return $response;

}

David J

Highlighted
Employee

Re: addOrUpdate($contactInfo) - Always returns true

Note: I made a mistake earlier, and I was able to get this to return true of false based on the success or failure of either operation. This could easily be adjusted to return different messages for the add or update function so that you can tell which of the two processes actually failed.


 


I hope this helps. Please let me know if you have any questions. Thanks.

David J

Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured