HOWTO: More options with dropdown fields that get values from a function

sugarcrmdevelopers —  March 8, 2012 — 10 Comments

UPDATED 3/12/2012 – Changed below code snippet to use the bean functions instead of a raw SQL query.

Last summer I thru together this post which talked about using a function to supply the values for a dropdown rather than having them come from the statically defined application list strings. Digging into this further, I found that the function you define has a few more options you can leverage to help further customize the dropdown. Namely, here are the parameters in order that get passed to the function that you can call.

  • $focus, which is the object reference to the SugarBean object for the record you are viewing.
  • $name, the name of the field that uses this function.
  • $value, the current value of the field on page load.
  • $view, an object reference to the view object for the view you are calling.

So with this new knowledge, I came across this request on the forums…

I am using Sugar Enterprise 6.2.1 and I am trying to populate a custom drop down in the Accounts module that is populated from the database for all the contacts relating to the current account. I have added some custom code to the custom/include/language/en_us.lang.php file that populates the dropdown for all contacts and that all works fine. 

But where I come unstuck is when I try to filter the contacts by the current account because I don’t know how to access the account_id for the current record so that I can do something along the lines of: 

$myQuery = "SELECT CONTACT_ID FROM mydatabase.accounts_contacts WHERE account_id = "+$current_account_id

My question is how do you access the value of the account_id field(or any field on the record) on both creation and edit of a record(in this case an account) when populating a custom drop down.

We can define a function like this which will return the dropdown values we are looking for, changing from using a straight SQL query to the bean functions instead:

function getAccountContacts($focus)
    $list = array();
    $list[''] = '';

    foreach ($focus->contacts->getBeans() as $contact) {
        $list[contact->id] = $contact->name;

    return $list;

If you are using a version of Sugar earlier than 6.3, change the getBeans() call above to getBeans(new Contact()) above.

This makes the dropdown only show the contacts that are related to the current account.

10 responses to HOWTO: More options with dropdown fields that get values from a function


    That just got 4x cooler. 


    Shouldn’t it just be?

    function getAccountsContacts($focus) {
    $list = array();
    $list[”] = ”;
    foreach ($focus->contacts as $contact) {
      $list[contact->id] = $contact->name;

    return $list;
    upgrade safe… DB agnostic and utilizes the Sugar Beans!


    {if $ == “”}
                        {html_options name=”disposition” id=”disposition” options=$fields.disposition.options selected=”Scheduled Meeting”}
                        {html_options name=”disposition” id=”disposition” options=$fields.disposition.options selected=$fields.disposition.value}


    Hello on sidecard, it does not show the select value on “detailview” but on database it was stored.
    It does not use the function to retrieve the list on the “detailview”.
    I tried with process_record and after_retrieve logic hooks but they didn’t work the field it draw empty always.


      Are you referring to Sugar 7’s Sidecar framework? If you populate a drop down using a function in Sugar 7 then it needs to use Enum API. The process_record and after_retrieve hooks do not apply.

Trackbacks and Pingbacks:

  1. programmatori web - March 20, 2012

    programmatori web…

    […]SugarCRM Developer Blog » Blog Archive » HOWTO: More options with dropdown fields that get values from a function[…]…

  2. Transfer value of a field in a related subpanel - January 28, 2013

    […] […]

  3. How to display related field in search view as multiselect field - January 28, 2013

    […] […]

  4. Drop down from another table - February 5, 2013

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s