SugarCRM 7: Populating relate field when creating record from subpanel

In this example, I have a custom module which is related to the Opportunities module. On custom module subpanel in Opportunities, I want to create a new record and auto populate the Account relate field based on the Opportunity’s account selected. To do this:

1. Create the create-actions view in the custom module (custom/modules/custom_module/clients/base/views/create-actions/) and create the controller create-actions.js inside the folder.

2. We can get the parent details from and we can set the values for our new record from this.model.attributes.

 extendsFrom: 'CreateActionsView',
 initialize: function(options) {
     this._super('initialize', [options]);
 render: function() {
     // Check if there's related parent record
     if (!_.isEmpty( {
         // Populate hidden field account_id_c with opportunity's account_id (Account GUID);
         // Populate account_name_c with opportunity's account_name (Actual name of Account);

SugarCRM: Replace Alert Email Template variables using custom code

In this post I will share with you how to replace/parse an alert email template using custom code.
This is useful if we are sending emails using logic hook but needed to use email templates which contains variables that should be replaced by actual values of the record.

There are two types of Alert Email Template that we will work on. First is Email and secord is Workflow type.

1. Email Templates (Type is Email)
This is pretty simple and I found couple of tutorials for this.
(Lets assume that variables in the email template are for Contact)

// Call email template class
$template = new EmailTemplate();
// Retrieve email template using id
// Parse the body of email template
$template->body_html = $template->parse_template_bean($template->body_html, $contact->module_dir, $contact);

* We use the parse_template_function and we passed the following parameters:
– first is the body of the email template
– second is the module of the object we will use
– third is the contact object


2. Email Templates (Type is Workflow)
Now this is different from the first one because variables in this template are in this format (e.g. {::future::Cases::case_number::).
This is how we replace variables using the SugarCRM way.
– We need to get first the Email template

// Call email template class
$template = new EmailTemplate();
// Retrieve email template using id

– Now check the Base Module of the template (we use the Case module as example)

// Retrieve case
$case = new aCase();

* The Base Module is important so we know what object to pass

– Now use the codes below to call the function that parses this variables (You can do this also for subject)

// Include alert utils file
// Call this function to parse email body
$body = parse_alert_template($case, $template->body_html, );

* the alert_utils.php contains function that SugarCRM uses to construct and send Alerts on the Workflow (this is not a class)
– first parameter is the object
– second parameter is the body of the email template


SugarCRM: Dynamically make a field required based on another field

In this post, we will provide the steps on how to make a certain field required when a field is set to a specific value.
We will use Cases module in this example and we will set Description field to required when the Status field is set to Closed.

1. Go to custom/Extension/modules/Cases/Ext/Dependencies and create custom_dependency.php (You can set any name for the file)

2. Add the following codes on the file:

$dependencies['Leads']['status'] = array(
    'hooks' => array("edit"), // this is where you want it to fire
    'trigger' => 'true', // to fire when fields change
    'triggerFields' => array('status'), // field that will trigger this when changed
    'onload' => true, // fire when page is loaded
    'actions' => array( // actions we want to run, you can set multiple dependency action here
        'name' => 'SetRequired', // function to trigger
        'params' => array( // the params for the set required action
            'target' => 'description', // the field id
            'label' => 'description_label', // the field label id
            'value' => 'equal($status, "Closed")', // the SugarLogic for it to trigger if the field is required or not

Formula used here is the same as the formula you set for fields in Studio when you want to add dependency.

For complete reference on what actions you can use, click this link.

SugarCRM: Manage custom status on History and Activities subpanel

The status options of Activities modules (Calls, Meetings, Tasks) are built in sugar dropdowns and are hard coded so that they will appear in either Activities subpanel or History subpanel.

This post will help you on how to manage custom status and set the conditions if it should appear on Activities or in History subpanel.

In this example, we will add a new status “Cancelled” to Task module. When task is set to this, it should appear in History subpanel to indicate that no further action is required. Make sure to add the new status first on Dropdown Editor.

The Easiest way: (not upgrade safe)

1. Go to modules/Tasks/metadata/subpanels/ForActivities.php and update the where condition for the subpanel.

'where' => "(tasks.status != 'Completed' AND tasks.status != 'Deferred' AND tasks.status != 'Cancelled')",

* We don’t want to include Cancelled tasks in Activities subpanel

2. Go to modules/Tasks/metadata/subpanels/ForHistory.php and update the where condition as well

'where' => "(tasks.status='Completed' OR tasks.status='Deferred' OR tasks.status='Cancelled')",

* This time we want to include all Cancelled tasks to be fetched and shown in History subpanel

3. Run Repair and Rebuild and we’re done.

The Upgrade safe way:

1. We need to make a copy of ForActivities.php and ForHistory.php from modules/Tasks/metadata/subpanels/ in custom/modules/Tasks/metadata/subpanels/ if not existing. We will rename the files as ForActivities2.php and ForHistory2.php. (You can set the name whatever you want)

2. Follow instruction on the Easiest way part and apply this to our new custom files. :)

3. To make our changes work, we have to override the Activities and History subpanel setup and use our custom files. In this example, we will start with the Leads module.

Go to custom/Extension/modules/Leads/Ext/Layoutdefs/ and create a a file override_subpanel.php (You can set the name whatever you want) and add the following codes:

$layout_defs['Leads']['subpanel_setup']['activities']['collection_list']['tasks']['subpanel_name'] = 'ForActivities2';
$layout_defs['Leads']['subpanel_setup']['history']['collection_list']['tasks']['subpanel_name'] = 'ForHistory2';

You will notice that we are overriding the subpanel setup of Activities and History subpanel for Leads module which is set in modules/Leads/metadata/subpaneldefs.php

* We need to apply this on every module that we want our changes for Tasks in Activities and History subpanel.

4. Run Repair and Rebuild and we’re done.

Changes are applicable to Meetings and Calls module as well. You just need to follow the instructions and change the module name used for the directories.

Reference: Tips & Tricks – Custom Activity Status Dropdowns

SugarCRM: Database Tables

List of tables used by sugar:

email_addr_bean_rel – table that relates the record id and email address id. This is where the id of the parent is stored (ex. bean_id=Parent ID).

email_addresses – actual table where the email address is saved (email_addr_bean_rel.email_address_id =

saved_report – reports created in sugar

users_signatures – stores user’s email signatures


(Incomplete list. Continuously updated.)

SugarCRM: Add a Sub Menu to a Module in Main Menu

This is how you add a new option under a module in the Main Menu on top of the screen.

Lets say you want to add an option “Process Leads” under the Leads module.  To do this:

1. Create a new file in custom/Extension/modules/Leads/Ext/Menus. If the Menus folder doesn’t exist, just make one. We can name the file menu.php.

2. On menu.php file, input codes below:

global $module_menu;

// This will add the new option
$module_menu[]=Array("index.php?module=Leads&action=ProcessLeads", "Process Leads", "");

In the example above, we assume that the action ProcessLeads is an existing file under custom/modules/Leads with the name ProcessLeads.php but you can also map actions with files.

3.  Just run Repair and Rebuild then logout to see the changes.

SugarCRM: Custom Module Edit View Template


class Custom<MODULE_NAME>ViewEdit extends ViewEdit {
    * @see SugarView::display()
    protected function _displayJavascript() {
        // Call the parent handler

    * @see SugarView::display()
    public function preDisplay() {
        // Call parent


Get every new post delivered to your Inbox.