Pages

Monday, January 3, 2011

SugarCRM Customization: Drop Down Fields

Looking to create a drop down field in SugarCRM?  


No problem.  Simply go to the customization Studio and add a new field of type "Drop Down," making sure to assign or create the appropriate drop down list at field creation time.


Simple enough, but that only works for custom fields.  What about changing an existing, default field so as to make it behave like a drop down.  For example, we might want to change the Subject field for Cases or the Name field for Opportunities into drop downs. 


Doing so would help us ensure uniformity in the classification of support inquiries or revenue opportunities, which in turn improves data quality and simplifies the measurement of effectiveness.  Ultimately, one of the goals of CRM technologies is to simplify the process of measuring how well your business is doing and the quality of the data in the system has a significant impact on that function.


So, how do we convert these default text fields to drop down fields?  Lets take a look.
Our first step is to determine if we need to define a new drop down list or if we are going to use an existing one.  


The drop down list contains the values that a user will be allowed to pick from for the field in question.  Note that the user must select a value from that list and will not be able to manually type in a random value.  As a result, the selected drop down list must contain appropriate values or we must create a new list that does.


We will assume we need to create new lists, respectively named 'cases_name_list' and 'opportunity_name_list.'  To create a new list, follow these steps:


1. Access SugarCRM as an admin level user.
2. Select Admin > Dropdown Editor
3. Click Add Dropdown


Proceed to name the list and enter the desired values.  Click Save when finished and repeat the process for the second list.


Now that the lists are ready, we need to connect them to the respective fields and instruct SugarCRM to make the fields behave like drop down fields.


To accomplish this, we need to do three things: create the PHP file with the instructions, place the file in the appropriate location within the SugarCRM install folder and lastly, execute a Repair operation within SugarCRM.


So what does the instruction file look like?  It is actually quite simple.  Create a file named vardefs.ext.php and add the following PHP code to it:


<?php
$dictionary['Case']['fields']['name']['type'] = 'enum';
$dictionary['Case']['fields']['name']['options'] = 'case_name_list';
?> 

That is all we need for the instructions.  


Now save the file and place it in the following folder: /custom/Extension/modules/Cases/Ext/Vardefs.  If the directory does not exist, proceed to manually create it via Windows Explorer or other similar tool appropriate for your operating system.  That completes the second step.  


To finalize the process, access SugarCRM using an admin level account and select Admin > Repair > Quick Repair and Rebuild.  


Take a look at the Cases module and the name field should now display as a drop down when in edit mode.


To complete the exercise and apply the modification to the Opportunities module, make another file named vardefs.ext.php, but with the following instructions:


<?php
$dictionary['Opportunity']['fields']['name']['type'] = 'enum';
$dictionary['Opportunity']['fields']['name']['options'] = 'opportunity_name_list';
?>

Place this version of the file in /custom/Extension/modules/Opportunities/Ext/Vardefs and rebuild!

31 comments:

  1. Very good article. This does a bang-up job on the Edit View, but can this also be applied to the Search views as well?

    ReplyDelete
  2. if i wnat to get the value of dropdown on change like i go to edit and change the value the some other field should be updated with that drop down value like if i select B then the other input field should contain B if i select C then the other input field should contain C.
    can you please help me in this regard! ??

    ReplyDelete
  3. I think you are asking about dependent dropdowns.

    For example, if one dropdown has a list of car manufacturers and a second has a list of car models, the second one would change based on the choice made for the first.

    If so, you can do that in Sugar Pro. If you are using Community Edition, take a look at the DevToolKit libraries available on Sugarforge.org as they allow you to do that as well.

    ReplyDelete
    Replies
    1. yes sir i want to dependent populated dropdown and its values from database when we import any file or date then the value are also populated.how it can solve.
      thanks

      Delete
  4. what if the ext.vardefs.php is auto generated and won't allow additional directories?

    ReplyDelete
  5. @FastTrack

    You can actually name it anything you want. Sugar will read all the files in custom/Extension/modules//Ext/Vardefs not just specific file names.

    ReplyDelete
  6. Thanks so much. Your answers are so logical as opposed to the stuff (by design or otherwise) i have gotten from other programmer types

    ReplyDelete
  7. In opportunity module in the drop down list there is others if user clicks on others it should show a text box .This is my requirement how can we do this..Please help me..

    ReplyDelete
  8. @Keerthi

    There are a number of ways of addressing that need, all of which require a rather lengthy explanation which I unfortunately cannot get into via the medium due to other commitments.

    However, in a nutshell, what you need to do is create a custom edit view with the necessary custom HTML and JavaScript that introduces the behavior you describe.

    ReplyDelete
  9. Hello to everybody.

    I just wanna add to this post:

    You can add your own drop down list at path /custom/include/language/.lang.php.
    Without studio intervention and use vardefs to add it on your module views.


    Thanks.

    ReplyDelete
  10. Hi:

    I have Sugarcrm version 6.0, and created dropdown list.
    I do not use studio to create dropdown list, but put the dropdown list under custom/include/language/en_us.lang.php.

    my dropdown list is complex, it has 4 levels, and each level has over hunders items.

    If i click one item , all it sub items will be displayed on screen.
    but there is a problem, how can i make search and report module to process those list item?

    ReplyDelete
    Replies
    1. Apologies, but I don't understand what it is you have done or why it is you are creating the list externally.

      Delete
    2. Anru,

      Are you saying you HAVE implemented a list like this? Or you WANT to implement one? If you have implemented it would you mind sharing how? I could also use a similar functionality!

      Thanks!

      Delete
  11. Hi, I to would like to use this function as part of my search definition when filtering Contacts. (I am using the Search filter to get a subset of my Contacts using results from a related module. I can than use the default Email, Mass Update, Mail Merge etc actions on them) I have the following in my custom/modules/Contacts/metadata/SearchFields.php

    'primary_status_c' =>
    array (
    'query_type' => 'default',
    'operator' => 'subquery',
    'subquery' => 'SELECT cours_crs_f117ontacts_ida
    FROM `cours_crs_sale_contacts_c`
    INNER JOIN cours_crs_sale_cstm ON ( cours_crs_sale_cstm.id_c = cours_crs_sale_contacts_c.cours_crs_8f21rs_sale_idb )
    INNER JOIN cours_crs_sale ON ( cours_crs_sale.id = cours_crs_sale_contacts_c.cours_crs_8f21rs_sale_idb ) and cours_crs_sale_cstm.primary_status_c LIKE',
    'db_field' =>
    array (
    0 => 'id',
    ),
    ),

    and in custom/modules/Contacts/metadata/searchdefs.php

    'primary_status_c' => array (
    'type' => 'enum',
    'name' => 'primary_status_c',
    'label' => 'Primary Status',
    'default' => true,
    'width' => '10%',
    'options' => 'primary_status_list',
    ),

    The field displays as a drop-down box but DOES NOT load the primary_status_list to the field? any suggestions would be GREATLY appreciated

    ReplyDelete
  12. Angel,
    You have mentioned the DevToolKit for dependent views. I have been struggling to make it work for Cases module where I want to make the subject field determine the list for category selection. I have read the manual but no joy.
    Any chance you could do a mini tutorial on just that part for the Cases module?

    ReplyDelete
    Replies
    1. Thanks for the feedback. I'll give that a look and publish a tutorial on it when time permits.

      Delete
    2. I too would like some help with this. I followed the steps in the directions, however some items were unclear. In the example php code there is a line "'product_name_c' => 'product_name_list',". But there is no explanation as to what the product_name_list refers to. Also, there were no instuctions on deploying the dev tool kit. I followed the directory structure and copied the javascript and utils directories in the tool kit to var/www/sugar/custom/include, as well as creating the php file in the correct module, and restarted apache. There is no indication that I did that correctly, the dependent drop-down feature does not work, and there is no error logging to indicate why.

      Thanks for any help!

      Delete
    3. DevToolKit wasn't the subject of this post. It also doesn't discuss the topic of dependent drop downs. The instructions within this post are for converting a default text field into a drop down field and nothing further.

      Per my previous comment, when time permits, I will post something on DevToolKit.

      Lastly, I am not sure which example you are referring to as "product_name_list" is not used in my example. However, there is a section in this post that does talk about lists and how to create them. Perhaps that helps you. See this section:

      "The drop down list contains the values that a user will be allowed to pick from for the field in question. Note that the user must select a value from that list and will not be able to manually type in a random value. As a result, the selected drop down list must contain appropriate values or we must create a new list that does.


      We will assume we need to create new lists, respectively named 'cases_name_list' and 'opportunity_name_list.' To create a new list, follow these steps:


      1. Access SugarCRM as an admin level user.
      2. Select Admin > Dropdown Editor
      3. Click Add Dropdown


      Proceed to name the list and enter the desired values. Click Save when finished and repeat the process for the second list."

      Delete
  13. hi Angel,
    I have just implemented the Cases example as per this blog to change the subject line from a simple string to a dropdown list. I now have a warning in htmlentities() in file /include/ListView/ListViewData.php. The error is: Warning:htmlentities() expects parameter 1 to be string, array given in /include/ListView/ListViewData.php on line 507

    I think that because we have changed from a string to an array for subject field, this could be the cause? Please help how to go around fixing this warning.

    Thanks

    Anthony

    ReplyDelete
  14. hi Anthony,

    to solve that problem you need to find searchdefs.php file for that module and replace

    array (
    0 => 'name',
    ...

    with

    array (
    0 =>
    array (
    'name' => 'name',
    'label' => 'LBL_NAME',
    'type' => 'enum',
    ),
    )

    ReplyDelete
  15. Can this be accomplished in a module builder so when the package is deployed or redeployed the changes don't have to be made manually everytime?

    ReplyDelete
    Replies
    1. Albert,

      No, you cannot do it through Module Builder itself. However, you should be able to manually edit the corresponding vardefs.php within /custom/ModuleBuilder and said changes should then carry over when you deploy the module.

      Delete
  16. A custom drop downs of address fields example is shown in http://johndopenotes.wordpress.com/2013/01/08/sugarcrm-change-address-state-and-country-to-dropdown-menu/
    We built a Module Loader package so it can be installed or uninstalled in on-demand instances.
    The solution worked with a country_dom drop down, but every time the module record was edited it blanked the country address field (unless you re-selected it ...)

    ReplyDelete
  17. Is this solution upgrade safe? or it would cause problem when updating it to new version of sugar

    ReplyDelete
    Replies
    1. Yes, this customization is upgrade-safe.

      Delete
  18. Hi, I implemented this solution too, but the name field in listview became unclickable. Are you having this problem?

    ReplyDelete
    Replies
    1. Yes, this is a side-effect of changing the field type. You could try adding link => true as an attribute for the field, although I don't recall if that gets honored or not.

      Another option would be to alter the listview to change the field type back to the original type of varchar when rendered in that view. That would automatically cause it become a link again.

      Delete
    2. As a solution I add a new calculated field which take the value from name and then I add link = true.
      :)

      Delete
  19. how do i delete a dropdown that is created using the dropdown editor in suitecrm or sugarcrm ?

    ReplyDelete
    Replies
    1. You can't do it through the interface in Sugar. You have to manually edit the language file that contains them in ./custom/include/language/.

      Delete
  20. Hi,
    When using this, the displayed value once submitted is the "item name" rather than the "display name". Meaning the displayed value has _ in-between words as spaces are invalid. How to I make it so the "display label" is shown.

    ReplyDelete

Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.