Hi
I'm using Sugar 7.x. I have a custom Dropdown field, and I want the accounts names to be present in that dropdown list, which should be dynamically updated when new accounts are created.
I'm using Sugar 7.x. I have a custom Dropdown field, and I want the accounts names to be present in that dropdown list, which should be dynamically updated when new accounts are created.
create a dynamic drop-down to achieve this.
1: add yourFieldname.php file
path: \custom\Extension\modules\yourModule\Ext\Vardefs\yourFieldname.php
code: $dictionary["yourModule"]["fields"]["yourFieldname"] = array (
'required' => false,
'name' => 'yourFieldname',
'vname' => 'LBL_YOURFIELDNAME',
'type' => 'enum',
'required' => false,
'massupdate' => 0,
'comments' => '',
'help' => '',
'importable' => 'true',
'duplicate_merge' => 'disabled',
'duplicate_merge_dom_value' => '0',
'audited' => false,
'reportable' => true,
'len' => 70,
'size' => '20',
'function' => 'getDropdown',
);
In vardef i called a function 'getDropdown'
2: getDropdown.php
path: \custom\Extension\application\Ext\Utils\getDropdown.php
code: function getDropdown(){
static $dropDown = null;
if(!$dropDown){
global $db;
$query = "SELECT id,name FROM accounts where deleted = 0 order by name asc ";
$result = $db->query($query, false);
$dropDown = array();
$dropDown[''] = '';
while (($row = $db->fetchByAssoc($result)) != null) {
$dropDown[$row['id']] = $row['name'];
}
}
return $dropDown;
}
Must Quick Repair and Rebuild to see the changes.
Cheers!
Waleed
skype:waleeedraza
why is the $dropDown varibale type set as "static"?
regards
You forgot (or perhaps written before this took affect in later Sugar versions) to mention that you MUST set in your vardefs:
$dictionary["yourModule"]["fields"]["yourFieldname"]["options"] = '';
Options set for dropdowns created in Studio will override the function parameter. I get it you may be talking about creating one from scratch without Studio, but for those of us who don't want to write that, using studio is quickest and setting options in your vardef to empty string will force it to ping the rest api and fetch the data from the custom function you created.
create a custom dropdown list and give some dummy values on it
for example custom dropdown list field name is "new_account_list" under "Contacts" module.
and from custom\include\language\en_us.lang.php remove dummy values from dropdown list field "new_account_list" that is like below
$app_list_strings['new_account_list']=array (
'1' => '1',
'2' => '2',
'3' => '3',
);
remove above code completely and add below code
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('data/SugarBean.php');
global $db;
$bean = new SugarBean();
$sql = "SELECT id, name FROM accounts";
$result =$bean->db->query($sql, true);
while ($row =$bean->db->fetchByAssoc($result)) {
$role_var[$row['id']]=$row['name'];
}
$GLOBALS['app_list_strings']['new_account_list']=$role_var;
Perform Quick repair remove browser cache and see the result
Will the above solution works dynamically? I mean whenever new records are added to accounts module will they get appended to this dropdown?
also you can set filter on it like show all Accounts those type is supplier
A deviation of shaji's code rather than using sql directly you can use SugarQuery (much safer and is advised by Sugar)
$sq = new SugarQuery();
$sq->select(array('id','name'));
$sq->from(BeanFactory::getBean('Accounts'));
$result = $sq->execute();
$GLOBALS['app_list_strings']['new_account_list'] = array();
foreach((array) $result as $row)
{
$GLOBALS['app_list_strings']['new_account_list'][$row['id']] = $row['name'] ;
}
You can use a relate field to the accounts module to fetch all the account names which would also be up-to-date at all times.
Rolustech: SugarCRM Engineering
Website: www.rolustech.com
Ph (US): +1 - 310 - 492 - 5564
Ph (UK): +44 - 207 - 9938 - 524
Email: info@rolustech.com
Skype: rolustech
1. Account
2. Contact
If I select a Account than on dropdown of Contacts I have to show only Contacts those are belongs to that selected Account
How it can be done?
Any help will be highly appreciable
Other way can be to use:
app.api.buildURL("Contacts", null) + app.api.call("read" ... using filter