How to Create Email Field in SugarCRM’s Custom Module

  • Home
  • Blog
  • How to Create Email Field in SugarCRM’s Custom Module
HOW TO CREATE EMAIL FIELD IN CUSTOM MODULE

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

<?php
$module = '<module_name>';
$dictionary[$module]['fields']['email1'] = array(
    'name'        => 'email1',
    'vname'        => 'LBL_EMAIL',
    'group'=>'email1',
    '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',
    'vname' => 'LBL_EMAIL_ADDRESS_PRIMARY',
    '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',
    'reportable'=>false,
    'unified_search' => true,
    'rel_fields' =>array('primary_address' => array('type'=>'bool')),
    );
$dictionary[$module]['relationships'][strtolower($module).'_email_addresses'] = array(
    'lhs_module'=> $module,
    'lhs_table'=>strtolower($module),
    'lhs_key' => 'id',
    'rhs_module'=> 'EmailAddresses',
    'rhs_table'=> 'email_addresses',
    'rhs_key' => 'id',
    'relationship_type'=>'many-to-many',
    'join_table'=> 'email_addr_bean_rel',
    'join_key_lhs'=>'bean_id',
    'join_key_rhs'=>'email_address_id',
    'relationship_role_column'=>'bean_module',
    'relationship_role_column_value'=>$module
    );
$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',
    'relationship_type'=>'many-to-many',
    'join_table'=> 'email_addr_bean_rel',
    'join_key_lhs'=>'bean_id',
    'join_key_rhs'=>'email_address_id',
    'relationship_role_column'=>'primary_address',
    'relationship_role_column_value'=>'1'
    );

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);
$this->emailAddress->handleLegacyRetrieve($this);
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);

parent::save($check_notify);
$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, 
override_email,'','','','',$this->in_workflow);
}
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.