How to make related field as Dropdown list in Suite CRM 7

Hi i`ve been trying to make a dropdown menu by fetching values from the relate module. In the path i have added the code as mentioned below.

Custom/Extension/Modules/mymodule/Ext/Vardefs/vardefs.php

$dictionary['mymodule']['fields']['custom_field'] = array(
'name' => 'custom_field',
'dbType' => 'non-db',
'source' => 'non-db',
'type' => 'relate',
'vname' => 'LBL_CUSTOM_TYPE',
'link' => 'accounts',
'module' => 'Accounts',
'rname' => 'account_type',
'studio' => 'visible',
);

but its not working. Am i missing something?
Thank you.

I have bookmarked this solution

https://web.archive.org/web/20140116145925/http://www.eggsurplus.com/home/content/populate-a-dropdown-from-the-database/

which is more powerful, with any SQL available to you.

But your attempted solution is simpler, so if anyone else knows how to fix it, please say how (I’m afraid I don’t know)…

1 Like

Thanks for the input.
ill try to fiddle with it a little more and if it doesn't work, i guess ill have to do it with that solution.

Cheers.

Hi! You can implement it creating a new field type enum and set their values from a function instead of options.

For example, I will add it a related field and enum. The idea is to create a field grilla_resultado which will be add it to my editviewdefs.php instead of gcoop_grilla_name.

        'gcoop_grilla_id' => array (
            'name' => 'gcoop_grilla_id',
            'type' => 'id',
            'len' => 36,
        ),
        'gcoop_grilla_name' => array (
            'source' => 'non-db',
            'name' => 'gcoop_grilla_name',
            'vname' => 'LBL_GCOOP_GRILLA_NAME',
            'type' => 'relate',
            'len' => '255',
            'id_name' => 'gcoop_grilla_id',
            'module' => 'gcoop_Grilla',
            'link'=>'gcoop_grilla_link',
            'join_name'=>'gcoop_grilla',
            'rname' => 'name',
        ),
        'gcoop_grilla_link' => array (
            'name' => 'gcoop_grilla_link',
            'type' => 'link',
            'relationship' => 'gcoop_grilla_gcoop_gestionobjetivos',
            'link_type'=>'one',
            'side'=>'right',
            'source'=>'non-db',
        ),
        'grilla_resultado' => array(
             'name' => 'grilla_resultado', 
             'type' => 'enum',
             'len' => 255,
             'vname' => 'LBL_GRILLARESULTADO',
             'audited' => false, 
             'function' => array(
                 'name' => 'get_grillas',
                 'include' => 'modules/gcoop_GestionObjetivos/Helper.php',
             ),
        ), 

Then, you need to create a file called Helper.php inside of your module and defined a new function called get_grilas. Your function should return an array with key and value items.

function get_grillas($focus, $field, $value, $view)
{
    $query = "SELECT id,
        name
        FROM gcoop_grilla
        WHERE
        confirmado = 1
        AND deleted = 0";
    $db = DBManagerFactory::getInstance();
    $resultado_query = $db->query($query);
    $resultados = array();

    while ($row = $db->fetchByAssoc($resultado_query))
    {
        $resultados [] = $row;
    }
    $arr_resultados = array('' => '<Seleccionar>');

    if ((!empty($resultados)) && (is_array($resultados)))
    {
        foreach ($resultados as $resultado) {
            $arr_resultados[$resultado['id']] = $resultado['name'];
        }
    }
    return $arr_resultados;
}

Of course, the only problem, is that your relationship field would not be filled with the value of your dropdown.
So, you need to implement the save() function on your Bean of your module.

Your save function should be something like this:

public function save($check_notify = false)
{
    LoggerManager::getLogger()->debug("IImplemts saving Bean GestionOjetivos {$this->id}");
    $this->gcoop_grilla_id = $this->grilla_resultado;
    $result = parent::save($check_notify);
    return($result);
}

Hope it helps!