Get contact email on after_save hook

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.