Populate dropdown with a DB query

Hi gents

I know, this topic has been discussed here a few times. I also googled it and came always to the same result:

Unfortunately, this is not working for SuiteCRM 8x anymore. I tried it. I went also through the documentation and was thinking about a backend handler. But I don’t need to calcuated on the fly the content of the dropdown.

What I did so far:

I create getOutboundMails.php in custom\Extension\application\Ext\Utils:

<?php

$db = DBManagerFactory::getInstance();

function getOutboundMails() {
	
    static $OutboundMails = null;
	if(!$OutboundMails){
				
		$query = 'SELECT id, name FROM outbound_email WHERE deleted=0';
		$result = $db->query($query);

		$OutboundMails = array();
		$OutboundMails[''] = '';

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

	}
	return $OutboundMails;
}

Then I created a custom varchar field in custom\Extension\modules\FP_events\Ext\Vardefs:

<?php

// created: 2025-01-29 11:29:06
$dictionary['FP_events']['fields']['outbound_mail_id_c']['inline_edit']='1';
$dictionary['FP_events']['fields']['outbound_mail_id_c']['labelValue']='Outbound E-Mail';
$dictionary['FP_events']['fields']['outbound_mail_id_c']['function']='getOutboundMails';

?>

Then I did a Quick Repair & Restore.

When I open the detailview or editview it shows me only “array” (see screenshot):

Screenshot 2025-01-29 155158

Any ideas, how the get it working in SuiteCRM 8.7.1 with PHP 8.1.31?

Cheers,
Carsten

Check if you need to clear symfony cache too.

I did clear the cach useing bin/console cache:clear, twice. No luck.

I crashed SuiteCRM. I found somehow the following error message :

PHP Fatal error:  Uncaught Error: Class "DBManagerFactory" not found in /webroot/www/suitecrm_dev/public/legacy/custom/application/Ext/Utils/custom_utils.ext.php:6
Stack trace:
#0 /webroot/www/suitecrm_dev/public/legacy/include/utils.php(5702): include_once()
#1 /webroot/www/suitecrm_dev/public/legacy/include/entryPoint.php(109): require_once('/webroot/...')
#2 /webroot/www/suitecrm_dev/config/bootstrap.php(70): require_once('/webroot/...')
#3 /webroot/www/suitecrm_dev/bin/console(30): require('/webroot/...')
#4 {main}
  thrown in /webroot/www/suitecrm_dev/public/legacy/custom/application/Ext/Utils/custom_utils.ext.php on line 6

After resetting it to the default code, the system was working again:

<?php

// $db = DBManagerFactory::getInstance();

function getOutboundMails() {
	
    global $db;
	
	static $OutboundMails = null;
	if(!$OutboundMails){
				
		$query = 'SELECT id, name FROM outbound_email WHERE deleted=0';
		$result = $db->query($query);

		$OutboundMails = array();
		$OutboundMails[''] = '';

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

	}
	return $OutboundMails;
}

So this might be the hint, why it isn’t working?

Try returning a string instead of an array. See if it appears on screen.

If it does, I seem to recall that dropdown lists are stored in the format ‘option1|option2|option3’, try that. But maybe I am misremembering.

If you can examine the data for another dropdown you might discover the exact format that is expected.

I fixed it, exactly the way you proposed @pgr :slight_smile:

the issue was, the field type was a textfield and not a selection field (dropdown). After updateing it in the db (changeing it to varchar(100) in the module_table_cstm as well as changeing it in the fields_meta_data) and gave it a default selection list, it works like a charm.

1 Like