How can I populate a dropdown box automatically with the data in a table of sugarcrm?

StackOverflow https://stackoverflow.com/questions/9543583

  •  04-12-2019
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top