Tuesday, 3 September 2013

Add a new Language in sugarcrm

Note: You can also set the default language in the config.php. This value is used if a user does not specify a language while logging into the application.
Note: In the include/language/<new>.lang.php file, you will see that there are two global arrays defined. The $app_list_strings array is actually an array of arrays. The key values in this array must not be changed. There are comments and examples in the code that should keep you on track.

Dependent Dropdown in SugarCRM (SugarCE)


In order to make dependent dropdown, first you need to create 2 independent dropdown list from sugar admin panel. Let's say test_country and test_city with Item Name as below:

For test_country menu:
Item Name                    Display Name
AAA                             Thailand
BBB                              Nepal

For test_city menu:
Item Name                   Display Name
AAA_1                        Bangkok
AAA_2                        Phuket
BBB_1                         Lumbini
BBB_2                         Pokhara

Here you can see that if Item Name for country is AAA then the corresponding Item Name for city is AAA_1, AAA_2 i.e. it starts with AAA.

Now, add these dropdown fields in the corresponding edit view and detail view.
I am making dependent dropdown for 'test_test_module' ie my custom module.

Let's make a javascript file, say test_test_module.js. I have kept this file inside custom/modules/test_test_module folder.
/***********************************************************************/
//alert('hello');
var arr;
function Check() {
if(document.EditView.test_city|| document.EditView.test_country) {
      var test_city= document.EditView.test_city.options;
      arr = new Array;
      for(i=0; i< test_city.length; i++) {
         arr.push( test_city[i].value, test_city[i].text);
      }
  }
    initData();
}
function initData(){
 var current_p= document.EditView. test_country;
 var code_p = current_p.value;
 var current_v= document.EditView.test_city;
 var code_v = current_v.value;
 var code_v_idx = 0;
 var select_ticket = document.EditView.test_city.options;
 select_ticket.length=0;
 var l = 0;

 for(k=0; k<arr.length; k+=2) {
   if(arr[k].substr(0,3) == code_p || arr[k] == '') {
  select_ticket.length++;
  select_ticket[select_ticket.length-1].value = arr[k];
  select_ticket[select_ticket.length-1].text = arr[k+1];
  if(code_v == arr[k]){
      code_v_idx = l;
 }
  l++;
  }
 }
 if(code_p == ''){
  select_ticket[select_ticket.length-1].value = '';
 select_ticket[select_ticket.length-1].text = 'Select from DD1';
 }
 document.EditView.test_city.selectedIndex = code_v_idx;
}
if (window.addEventListener){
window.addEventListener("load", Check, false);
}else if (window.attachEvent){
window.attachEvent("onload", Check);
}else if (document.getElementById){
window.onload=Check;
}
/***************************************************************/
Inside templateMeata array in custom/modules/test_test_module/metadata/editviewdefs.php, define the location of the javascript file as below,
we can do it in two way:-
a>
'templateMeta' =>
    array (
    'includes' =>
      array (
        0 =>
        array (
          'file' =>'custom/modules/test_test_module/test_test_module.js',
        ),
      ),
      'maxColumns' => '2',
   
note : - Here only 'includes' key is important. but for better understanding extra code given above
b>
$viewdefs['test_test_module']['EditView']['templateMeta']['includes'][]['file'] = 'custom/modules/test_test_module/test_test_module.js';

last step :
In the same file i.e. editviewdefs.php, you will find dropdown menus you created i.e. test_country and test_city. Call the javascript function initdata() in test_country menu as below:
   array (
            'name' => 'test_country',
            'label' => 'LBL_AMITESH_DROPDOWN',
            'displayParams' => array (
                 'javascript' => 'onchange="initData();"'
                 ),
          ),
      
Since you want to change the value of city when the value of country is changed, you need to call the javascript function only from the test_country menu.




all d best :)




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', );