Dropdowns using SugarLogic Formulas

sugarcrmdevelopers —  August 8, 2012

I found the easiest way to write this was a very specific example, adding a custom Dropdown field on the Contacts module that would copy the already-existing dropdown value of a related Account. You should be able to substitute any fields that are related, really, with only a basic understanding of vardefs (and you may not even need that).

Goal: Add a field on the Customers module to display the related Account’s Type field.
Challenge: SugarCRM Studio doesn’t allow you to use Calculated SugarLogic Formulas on Dropdown-type fields
Solution: upgrade-safe changes, via PHP code, to the field’s vardefs.

  1. Use studio to create a new dropdown field, have it using the same dropdown values as the related field. Save the field and place it on the Edit and Detail Views.
  2. Use studio to create a new text field. Enable the Calculated field and use the tool to find your formula, probably related($accounts,”account_type”).
  3. Open the file system, browser to /custom/Extension/modules/Contacts/Vardefs and open the two new files in your text editor. You will find the following blurbs:For the calculated text field there will be something like:
    $dictionary['Contact']['fields']['account_type_text_c']['duplicate_merge_dom_value']=0;
    $dictionary['Contact']['fields']['account_type_text_c']['calculated']='true';
    $dictionary['Contact']['fields']['account_type_text_c']['formula']='related($accounts,"account_type")';
    $dictionary['Contact']['fields']['account_type_text_c']['enforced']='true';
    $dictionary['Contact']['fields']['account_type_text_c']['dependency']='';

    And for the new dropdown field you’ll probably only find one line:

    $dictionary['Contact']['fields']['account_type_c']['visibility_grid']='';

    Take the vardef values for the calculated field and mash them with the dropdown field, like so:

    $dictionary['Contact']['fields']['account_type_text_c']['duplicate_merge_dom_value']=0;
    $dictionary['Contact']['fields']['account_type_text_c']['calculated']='true';
    $dictionary['Contact']['fields']['account_type_text_c']['formula']='related($accounts,"account_type")';
    $dictionary['Contact']['fields']['account_type_text_c']['enforced']='true';
    $dictionary['Contact']['fields']['account_type_text_c']['dependency']='';
  4. Now run a repair & rebuild to view these changes. You’re of course welcome to delete the text field, we only used it to create and copy the formula.