Change relate to dropdown on EditView

Jun 30
2011

Ever wanted to display a relate field as a dropdown list on an EditView?  Here is how you can do it.

Assume you have a custom module that has a relate field to Accounts.  The module will have a field account_name and a field account_id.

In the vardefs.php for your module add the list of options to the account_id field:

  1. 'account_id' =>
  2. 'name' => 'account_id',
  3. ...
  4. 'options' => 'account_list',
  5. ),

Then in the metadata/editviewdefs.php change account_name to account_id and add a type =>’enum’ to it.

  1. 'name' => 'account_id',
  2. ...
  3. 'type' => 'enum',
  4. ),

Then lastly add some code at the top of your modules class file (my_class.php) to create and populate the list.  You can of course add more filters to the where clause to display a subset of related records.

  1. global $db, $app_list_strings, $current_user;
  2. if ($_REQUEST['action'] == 'EditView')
  3. {
  4. $app_list_strings['account_list'] = array(''=>'');
  5. $query = "SELECT a.id,a.name FROM accounts a WHERE a.deleted=0 ORDER BY a.name";
  6. $res = $db->query($query);
  7. while ($row = $db->fetchByAssoc($res))
  8. $app_list_strings['account_list'][$row['id']] = $row['name'];
  9. }

Now do a quick repair on your module to remove the cached vardefs and EditView template and you should see a dropdown list where the select box used to be 🙂

For custom modules built in module builder you should edit the vardefs.php and metadata/editviewdefs.php inside custom/modulebuilder/packages/<package>/modules/<module>/ and the class file in custom/modulebuilder/builds/<package>/SugarModules/modules/<module>/<module>.php.

If the module is a built in module you will have to make the vardefs changes in an extended vardefs file in custom/Extension/modules/<module>/Ext/Vardefs/, change the metadata in custom/modules/<module>/metadata/editviewdefs.php and create the list in an after_retrieve logic hook.

 

 

Leave a Reply

You must be logged in to post a comment.

Not unto the swift…

The race is not to the swift or the battle to the strong, nor does food come to the wise or wealth to the brilliant or favor to the learned; but time and chance happen to them all.
— Ecclesiates 9:11 (NIV)