1

I'm writing some code for sugarCRM, and I'd like to notify employees via email. I'm using the active record model to get employees, but their emails are nowhere to be found.

$employees = BeanFactory::getBean('Employees')->get_full_list();

How do I get the active employee emails via this interface? I need to pluck them out of the records and add them to the email I'm sending out.

Derrek Bertrand
  • 596
  • 7
  • 13

3 Answers3

2

I also faced this situation but I came up with the below solution. Hope it works for you. Cheers!!

<?php

$userId=array('2132131312323232','323232323232323'); //array of users id's
foreach($userId as $key => $val) {
 $email_query = "select E.email_address from email_addr_bean_rel EB inner join email_addresses E on EB.email_address_id = E.id WHERE EB.bean_id='".$val."' and E.deleted = 0"; 
 // Your implementation
}
?> 
Sachin I
  • 1,500
  • 3
  • 10
  • 29
1

Referencing sAcH and this the schema documentation, I ended up writing the query below. It can probably be cleaned up a lot, but it functions and that's what I care about right now.

It returns the primary email of all active, non-deleted employees, effectively making an email list for your active users:

select e.email_address
from email_addr_bean_rel eb
    inner join email_addresses e
    on eb.email_address_id = e.id
where eb.bean_id in
    (select id
        from users
        where deleted = 0 and employee_status = 'Active')
    and e.deleted = 0 and eb.primary_address = 1
    and eb.deleted = 0 and eb.bean_module = 'Users'

Important to note, is that there are two fields where a user can be active: status and employee_status. This checks the latter; just note that there are two 'status' fields, not one!

Derrek Bertrand
  • 596
  • 7
  • 13
1

To save user's email address use this:

$sea = new SugarEmailAddress;
$sea->addAddress($bean->email1, true);
$sea->save($newUser->id, "Users");

To retrieve primary email address use this:

$primary_email=$user->emailAddress->getPrimaryAddress($user);

Hope this will help you... :)

Checkout this for more: SugarCRM Developer

Indrasinh Bihola
  • 2,094
  • 3
  • 23
  • 25