How to Create Email Field in Custom Module

How to Create Email Field in SugarCRM’s Custom Module



Creating “Email Field” in Custom Module is simple and the functionality can be achieved in five steps explained 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 module’s 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 bean by pasting the below given code 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.


Hope this post was helpful! Find more Sugar tips and tricks here

email field in custom module

Rolustech is a team award winning certified SugarCRM experts taking pride in successfully implementing SugarCRM for more than 500 firms worldwide.


Get in touch

Please leave this field empty.