Auto populate email from contact module into invoice module


I’m trying to auto populate some fields from a relate field.
I have got it all working except for one field: the email address field.
I’ve tried finding the different database field names but it just won’t work.

$dictionary[‘AOS_Invoices’][‘fields’][‘billing_contact’][‘populate_list’] = array(‘id’, ‘company_c’, ‘phone_work’, ‘address_c’, ‘email1’);
$dictionary[‘AOS_Invoices’][‘fields’][‘billing_contact’][‘field_list’] = array(‘billing_contact_id’, ‘clientcompany_c’, ‘contactofficephone_c’, ‘clientaddress_c’, ‘clientemail_c’);

Any ideas?

I cant say I have pulled the email address on a relate field before.

I would assume that it is not working due to the fact that the email address is held in its own table and not within the record that you are related to. so its not find the field. It might mean you need to go a couple steps further to pull the email address into the record.

without looking at how sugar is dealing with this it would be hard to determine the best way. you could create another ajax call to get the email address details on the return of all the related data above.

I was thinking about making a plan text field in contacts called emails and grabbing that one and somehow linking those two together?

You could build a hook which adds the first email address to that field on save. this would then let you grab the email address that way. that would be easier than making a ajax call on the return of the data. you would need to handle how it deals with a user adding two email addresses, but then on that you could just take the primary email address each time.


That’s fine with just one email address, we never add more than one ever so I even got rid of the add button.

That would work very well when adding a contact from the contacts module, only thing is I use logic hooks to add contacts from directly within an invoice form, so how can I use a logic hook to add an email address to that contact too as that is also not working, every other field is though?

When adding contacts from within a invoice do you use a SQL query or create via the beanFactory?

If you use the BeanFactory then the logic hooks in that module(which is contacts) would fire when the Bean is saved which then should pull in the email address to that new field.

That sounds great!

I’ve tried to create the logic hook but it’s not working:

$bean = BeanFactory::getBean(‘Contacts’, $id );
$bean->email1 = $bean->email_c;

Any ideas why it’s not working?


Sugar handle email and module relationships in a different way.

Have a look at code inside save method of class - SugarBean - file :- /data/SugarBean.php file.

You can see code like this …

$this->emailAddress->save($this->id, $this->module_dir);

EmailAddress is a module. It extends SugarEmailAddress (/include/SugarEmailAddress/SugarEmailAddress.php)

You can use it to save email address.


Alpesh Savaliya

Thanks, I did it in the end with a query to add the address to the email_addresses table and then make it related to the contact with the email_add_bean_relate table

1 Like

Hi ajgisme,

You wrote: “Thanks, I did it in the end with a query to add the address to the email_addresses table and then make it related to the contact with the email_add_bean_relate table”

How were you able to do this? Thanks!

I used two separate SQL queries to do this.

Like this:

$idemailnew0 = create_guid();
$queryc_email0 = "INSERT INTO email_addresses (id, email_address, email_address_caps, date_created, date_modified) VALUES ('".$idemailnew0."', '".$bean->clientemail_c."', '".$bean->clientemail_c."', now(), now());";
$resultc_email0 = $db->query($queryc_email0, true,"Error setting tasks entry: ");
$idemailrelatenew0 = create_guid();
$queryc_emailrelate0 = "INSERT INTO email_addr_bean_rel (id, email_address_id, bean_id, bean_module, date_created, date_modified, primary_address) VALUES ('".$idemailrelatenew0."', '".$idemailnew0."', '".$bean->billing_contact_id."', 'Contacts', now(), now(), '1');";
$resultc_emailrelate0 = $db->query($queryc_emailrelate0, true,"Error setting tasks entry: ");

I don’t think it’s great but it worked.

1 Like