How to Create Email Field in SugarCRM’s Custom Module

By
855 views

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

<?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 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);
$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.

 

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.

 

Need Help? Get Free Consultation


Please leave this field empty.

Get in touch



Please leave this field empty.

CONTACT INFORMATION