How to Create Email Field in SugarCRM’s Custom Module

Last Modified Date

Creating an “Email Field” in a Custom Module is simple and the functionality can be achieved in the five steps enumerated below:

1.   Copy the following code and paste it in custom/Extension/modules/<module_name>/Ext/Vardefs/field_email.php

$module = '<module_name>';
$dictionary[$module]['fields']['email1'] = array(
    'name'        => 'email1',
    'vname'        => 'LBL_EMAIL',
    'type'        => 'varchar',
    'function'    => array(
'name'        => 'getEmailAddressWidget',
'returns'    => 'html'
    'source'    => 'non-db',
    'studio' => array('editField' => true),
$dictionary[$module]['fields']['email_addresses_primary'] = array(
    'name' => 'email_addresses_primary',
    'type' => 'link',
    'relationship' =>strtolower($module).'_email_addresses_primary',
    'source' => 'non-db',
    'duplicate_merge' => 'disabled',
$dictionary[$module]['fields']['email_addresses'] = array (
    'name' => 'email_addresses',
    'type' => 'link',
    'relationship' =>strtolower($module).'_email_addresses',
    'source' => 'non-db',
    'vname' => 'LBL_EMAIL_ADDRESSES',
    'unified_search' => true,
    'rel_fields' =>array('primary_address' => array('type'=>'bool')),
$dictionary[$module]['relationships'][strtolower($module).'_email_addresses'] = array(
    'lhs_module'=> $module,
    'lhs_key' => 'id',
    'rhs_module'=> 'EmailAddresses',
    'rhs_table'=> 'email_addresses',
    'rhs_key' => 'id',
    'join_table'=> 'email_addr_bean_rel',
$dictionary[$module]['relationships'][strtolower($module).'_email_addresses_primary'] = array(
    'lhs_module'=> $module,
    'lhs_table' =>strtolower($module),
    'lhs_key' => 'id',
    'rhs_module' => 'EmailAddresses',
    'rhs_table' => 'email_addresses',
    'rhs_key' => 'id',
    'join_table'=> 'email_addr_bean_rel',

2.   Go to your custom modules directory i.e. modules/<module_name>/

3.   Open it’s bean file: modules/<module_name>/<module_name>.php and paste the following code in constructor under parent::<module_name>();

$this->emailAddress = new SugarEmailAddress();

4.   Next: override, retrieve, and save methods of the bean by pasting the code given below into that file. These changes will be non-upgrade safe in default modules and upgrade safe in custom modules.

public function retrieve($id = -1, $encode=true)

$ret_val = parent::retrieve($id, $encode);
return $ret_val;
public function save($check_notify=false)
$ori_in_workflow = empty($this->in_workflow) ? false : true;
$this->emailAddress->handleLegacySave($this, $this->module_dir);

$override_email = array();
if(!empty($this->email1_set_in_workflow)) {
$override_email['emailAddress0'] = $this->email1_set_in_workflow;
if(!empty($this->email2_set_in_workflow)) {
$override_email['emailAddress1'] = $this->email2_set_in_workflow;
if(!isset($this->in_workflow)) {
$this->in_workflow = false;
if($ori_in_workflow === false || !empty($override_email)){
$this->emailAddress->save($this->id, $this->module_dir, 
return $this->id;

5.   Execute repair and rebuild. [Check out our complete Sugar repair guide]


Hope this post was helpful! Learn more SugarCRM tips and tricks.


Rolustech is an award-winning team of certified SugarCRM developers taking pride in successfully implementing SugarCRM customizations for more than 500 firms worldwide. Get in touch for a Free CRM Consultation.

Need Help? Get a Free Consult

By clicking you agree to our Terms and Conditions

Send me news and updates

How can we help you?

Contact Sales

  • US +1 415 800 4489
  • UK +44(0)207-993-8524
  • PK +92(0)423-640-7166

Looking For a Job?