Get contact email on after_save hook

I need each time a contact is registered to create a record in an external service. For this purpose I have created the following hook

public/legacy/custom/modules/Contacts/logic_hooks.php

$hook_array['after_save'][] = [
     '2',
     'Create contact in External service',
     'custom/modules/Contacts/logic_hooks/ExternalService.php',
     'ExternalService',
     'create_contact'
];
<?php

class ExternalService
{
    public function create_contact($bean, $event, $arguments)
    {
        $api = new \name\external_service();

        $data = [
            "attributes" => [
                'FNAME' => $bean->first_name,
                'LNAME' => $bean->last_name,
                "COUNTRY" => $bean->primary_address_country,
                "CITY" => $bean->primary_address_city,
            ],
            "updateEnabled" => false,
            "email" => $bean->email,
            "ext_id" => "10"
        ];

        $api->createContact($data);
    }
}

External service

<?php

namespace brevo;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;

class brevo
{
    private string $base_url;
    private string $api_key;
    private Client $client;

    public function __construct()
    {
        global $sugar_config;

        $this->base_url = $sugar_config['es']['base_url'];
        $this->api_key = $sugar_config['es']['api_key'];
        $this->client = new Client([
            'base_uri' => $this->base_url,
            'headers' => [
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',
                'api-key' => $this->api_key,
            ]
        ]);
    }

    public function createContact(array $data): string
    {
        $endpoint = '/contacts';

        try {
            $response = $this->client->post('https://api.es.com/v3/contacts', [
                'json' => $data
            ]);

            if ($response->getStatusCode() === 201) {
                return "Contact created successfully";
            }

            return "Failed to create contact. Status code " . $response->getStatusCode();
        } catch (RequestException $e) {
            return "RequestException caught: " . $e->getMessage();
        } catch (GuzzleException $e) {
            return "GuzzleException caught: " . $e->getMessage();
        }
    }
}

The problem is that I need the email but when I try to get it from the $bean in the hook there is no value.

How do I get the contact’s main email in an after_save hook?

Thanks in advance

You can try to get it from $bean->email1 which is a proxy field providing the primary email, but it’s not always filled, and it’s not always what you want.

The real solution is to navigate the email_addr_bean_rel relationship and work with the list of related email_addresses.

Here is a sample bit of SQL that might help you understand the relationship:

 SELECT ea.email_address 
 FROM email_addresses ea                 
 LEFT JOIN email_addr_bean_rel ear 
 ON ea.id = ear.email_address_id                 
 WHERE ear.bean_module = 'Contacts'                 
 AND ear.bean_id = 'ed6b0688-54bf-3769-bc06-5ad0aab95e73'                 
 AND ear.deleted = 0                 
 AND ea.invalid_email = 0                 
 ORDER BY ear.primary_address DESC LIMIT 0,1

But you don’t have to use SQL, you can use beans and navigate the relationship. Something similar to this post, without deleting the emails, of course.

Thank you very much for answering. I will see to implement it