Tuesday, 3 September 2013

HOWTO: Create a Flex Relate for other modules

You may recall seeing this field in the various activity modules in Sugar:
This is what’s known as the “Flex Relate” field, which allows you to relate a record to one in a different module that you specify. This allows you to create a relationship where the target entity is flexible, which allows you to represent all sorts of business logic clearly. A great example of this the various activity entities in the app ( Calls, Meetings, Tasks ), which make it so you can relate the activity to one of many different record types.
The only downside of this field, is there’s no good way to build it using Module Builder or Studio ( or least in a very useful way ). However, it’s a pretty easy code customization you can do which is upgrade-safe. Let’s look at how.
We’ll assume we made a new custom module via Module Builder named test_flexparent, which will have a flex relate field that can relate to either the Contacts or Leads module. To do this, we must define the fields and relationships which will be needed to make this happen; we can do this by adding the file custom/Extension/modules/test_flexparent/Ext/Vardefs/flexrelate.php with the following contents:

<?php
 
$dictionary['test_flexparent']['fields']['parent_name'] = array(
'required' => false,
'source' => 'non-db',
'name' => 'parent_name',
'vname' => 'LBL_FLEX_RELATE',
'type' => 'parent',
'massupdate' => 0,
'comments' => '',
'help' => '',
'importable' => 'true',
'duplicate_merge' => 'disabled',
'duplicate_merge_dom_value' => '0',
'audited' => false,
'reportable' => true,
'len' => 25,
'size' => '20',
'options' => 'test_flexparent_options',
'studio' => 'visible',
'type_name' => 'parent_type',
'id_name' => 'parent_id',
'parent_type' => 'test_flexparent_options',
);
 
$dictionary['test_flexparent']['fields']['parent_type'] = array(
'required' => false,
'name' => 'parent_type',
'vname' => 'LBL_PARENT_TYPE',
'type' => 'parent_type',
'massupdate' => 0,
'comments' => '',
'help' => '',
'importable' => 'true',
'duplicate_merge' => 'disabled',
'duplicate_merge_dom_value' => 0,
'audited' => false,
'reportable' => true,
'len' => 255,
'size' => '20',
'dbType' => 'varchar',
'studio' => 'hidden',
);
 
$dictionary['test_flexparent']['fields']['parent_id'] = array(
'required' => false,
'name' => 'parent_id',
'vname' => 'LBL_PARENT_ID',
'type' => 'id',
'massupdate' => 0,
'comments' => '',
'help' => '',
'importable' => 'true',
'duplicate_merge' => 'disabled',
'duplicate_merge_dom_value' => 0,
'audited' => false,
'reportable' => true,
'len' => 36,
'size' => '20',
);
 
 
$dictionary['test_flexparent']['fields']['contacts'] = array(
'name' => 'contacts',
'type' => 'link',
'relationship' => 'test_flexparent_contacts',
'module'=>'Contacts',
'bean_name'=>'Contact',
'source'=>'non-db',
'vname'=>'LBL_CONTACTS',
);
 
$dictionary['test_flexparent']['fields']['leads'] = array(
'name' => 'leads',
'type' => 'link',
'relationship' => 'test_flexparent_leads',
'module'=>'Leads',
'bean_name'=>'Lead',
'source'=>'non-db',
'vname'=>'LBL_LEADS',
);
 
$dictionary['test_flexparent']['relationships']['test_flexparent_contacts'] = array(
'lhs_module' => 'test_flexparent',
'lhs_table' => 'test_flexparent',
'lhs_key' => 'parent_id',
'rhs_module' => 'Contacts',
'rhs_table' => 'contacts',
'rhs_key' => 'id',
'relationship_type' => 'one-to-many',
'relationship_role_column'=>'parent_type',
'relationship_role_column_value'=>'Contacts'
);
 
$dictionary['test_flexparent']['relationships']['test_flexparent_leads'] = array(
'lhs_module' => 'test_flexparent',
'lhs_table' => 'test_flexparent',
'lhs_key' => 'parent_id',
'rhs_module' => 'Leads',
'rhs_table' => 'leads',
'rhs_key' => 'id',
'relationship_type' => 'one-to-many',
'relationship_role_column'=>'parent_type',
'relationship_role_column_value'=>'Leads'
);
 
There are three parts to this file. The first part defines the three fields we have added to manage the relationship: parent_name, parent_id, and parent_type. We next add two link fields which will correspond to the relationships we’ll add for the links between this module and contacts and leads. Finally, we define the actual relationships themselves.
One more thing we need to do is add in the language files which specify the field’s string name as well as the dropdown. You can do this by adding a file named custom/Extension/application/Ext/Language/en_us.flexrelate.php.

<?php
 
$app_list_strings['moduleList']['test_flexparent'] = 'Flex Parent';
$app_list_strings['test_flexparent_options'] = array(
'' => '',
'Contacts' => 'Contact',
'Leads' => 'Lead',
);
 
Now after a Repair Relationships and a Quick Rebuild and Repair, the field will be available and you can add it to the module’s various views.
 
#troubleshoot1 :if it isn't work in custom/Extension/application/Ext/Language/en_us.flexrelate.php.  
 the place below written code in custom/Extension/include/Language/en_us.lang.php. in respect of above

 $GLOBALS['app_list_strings']['test_test_module_options'] = array( '' => '', 'Contacts' => 'Contact', 'Leads' => 'Lead', );

1 comment:

  1. http://developers.sugarcrm.com/wordpress/2011/05/16/howto-create-a-flex-relate-for-other-modules/

    ReplyDelete