How to add a contact to a target list through code?

I haven’t been able to find any code references to do this. Target lists don’t show up in studio either…

Try to find some online documentation about using Beans.

Basically, you need to fetch a “Contact” bean, fetch a “Target List” bean, and create a relationship between the two.

Below is code similar to what you will need (instead of linking to Account, you can add to “Target List”):


function GetOrCreateSimpleContact ($the_name, $the_account) {
    if ($the_name != "") {
        $person = BeanFactory::getBean("Contacts");
        $person = $person->retrieve_by_string_fields(array('full_name_c' => $the_name));
        if (is_null($person)) {
            $person = BeanFactory::newBean("Contacts");
            $person->full_name_c = $the_name;
            $person_id = $person->save();
            // add new person to account:
            $account = BeanFactory::getBean('Accounts')->retrieve_by_string_fields(array('name'=> $the_account));
            $person->load_relationship('accounts');
            $person->accounts->add($account->id);
            return $person;
        }
        return $person;
    }
    return null;
}

This code creates a new Contact when one doesn’t exist. Your code can be simpler if you know for sure that the Contact is pre-existing.

I hope this helps.