Populate a Dropdown list from the database

It will be done by simple steps,

Step 1: What you need to do is to create a custom util function and then tell your vardefs for that field to call the custom util function.

Path: custom/Extension/application/Ext/Utils/<any_name>.php add below your code as per requirement. For example,

function getAccounts(){
    static $accounts = null;
    if(!$accounts){
        global $db;
        $query = "SELECT id, name FROM accounts";
        $result = $db->query($query, false);

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

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

Step 2: To extend vardefs we need to create a file. custom/Extension/modules/<desired_module>/Ext/Vardefs/<anyname>.php

<?php
$dictionary["<MODULE_OBJECT_NAME>"]["fields"]["<FIELD_NAME>"][function"]='getAccounts';

Step 3: Now go to Admin > Repair > Quick Repair and Rebuild.

Note :

Here, <desired_module> means the module name you see in the URL, for example, Contacts, Leads, etc.
Here, <anyname> means the any file name for example, custom_fields, search_fields, etc.
Here, <MODULE_OBJECT_NAME> means module object for example Account,Contact, etc.

Hope you find this blog post helpful.

Feel free to add comments and queries, that helps us to improve the quality of posts.

You can contact us at info@infotechbuddies.com

Thank you.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *