4

I want to create a dropdown list in sugarcrm custom module and auto populate it using the data in the sugar database. Dropdown options should be fetched from table. Name column will be displayed as option display text and ID as option value.

Can any one help me with this?

Star
  • 3,222
  • 5
  • 32
  • 48
sabna
  • 67
  • 2
  • 5

2 Answers2

4

All of the dropdowns are built in a language file in custom/include/language and are stored in an array called $app_list_strings. Essentially what you would do is run your query in the language file and then use the results to build the array for that drop down list.

If you look through the existing examples you'll see something like this.

       $GLOBALS['app_list_strings']['drop_down_name'] = array(
       'dropdown_value'=>'Dropdown Display',
       'dropdown_value2'=>'Dropdown Display2',
       );

If you do the following:

       $new_array = array();
       while($row = $db->fetchByAssoc($result)) {
          $new_array[$row['key']] = $row['value'];
       }

       $GLOBALS['app_list_strings']['dropdown'] = $new_array;

You'll accomplish what you need

frosty
  • 769
  • 6
  • 18
  • 1
    i think $db = DBManagerFactory::getInstance($instanceName); $myQuery = "select PRO_CODIGO, PRO_DESCRIP from sugarcm.PROVINCIAS"; $Result=$db->query($myQuery); is missing in your reply. – Rodniko Mar 24 '13 at 05:36
  • 1
    You are correct, I didn't include the whole entire query for retrieving the data, just everything from that point forward – frosty Mar 28 '13 at 23:32
0

you can create function field. Inside your function, write logic for fetching data from database and then return it using associative array. See following code for reference:

Field definition :

$dictionary['MODULENAME']['fields']['FIELDNAME']['function'] = 'getActiveReleases'; 

Function :

function getActiveReleases()
{
    $query = "SELECT id, name FROM releases where deleted=0 and status='Active' order by list_order asc";
    $result = $GLOBALS['db']->query($query, false);

    $list = array();
    $list['']='';
    while (($row = $GLOBALS['db']->fetchByAssoc($result)) != null) {
        $list[$row['id']] = $row['name'];
    }

    return $list;
}
Star
  • 3,222
  • 5
  • 32
  • 48