Dependent Fields Not Listing MultiSelects

sugarcrmdevelopers —  September 29, 2011

Editor’s Note: This post comes from the blog of Sugar Partner and Open+ Developer Program member Profiling Solutions. In this post, developer Matt Poer give advice building dependent fields that use multiselects.

Whether or a bug, or a temporary limitation, you’ll find that the Dependent Field forumla editor in SugarCRM’s Studio doesn’t show MultiSelect fields. I suspect the reason is that the vardefs expect an equivalent value, but the values stored of a multiselect are a bit more difficult to check, because there are potentially several (or potentially none).

Here’s a simple work around using some custom javascript to achieve a similar effect.

For this example, we’ll be modifying the Contacts module to detect a particular value (“Other”) in a custom field (“client_needs_c”). If the field is found with the desired value, we’ll display and require a new text field (“client_needs_other_c”):

$viewdefs['Contacts']['EditView']['templateMeta']['includes'][]=array('file' => 'custom/modules/Contacts/psi_dependent.js');

Now create custom/modules/Contacts/psi_dependent.js with the following contents:

setTimeout('document.getElementById('client_needs_c').setAttribute('onchange','check_dependency(this.options)');',2000);
setTimeout('check_dependency(document.getElementById('client_needs_c').options)',2000);

function check_dependency(options){
    var field_value_to_check = "Other";

    var selObj = document.getElementById('client_needs_c');
    var i;
    for (i=0; i<selObj.options.length; i++) {
        if (selObj.options[i].selected && selObj.options[i].value == field_value_to_check) {
            document.getElementById('client_needs_other_c').parentNode.setAttribute('style','display:inline;');
            document.getElementById('client_needs_other_c_label').setAttribute('style','display:table-cell;');
            addToValidate('EditView', 'client_needs_other_c', 'varchar', true,'Client Needs (other)' );
        } else if (!selObj.options[i].selected && selObj.options[i].value == field_value_to_check){
            document.getElementById('client_needs_other_c').parentNode.setAttribute('style','display:none;');
            document.getElementById('client_needs_other_c_label').setAttribute('style','display:none;');
            removeFromValidate('EditView', 'client_needs_other_c')
        } else {
            // don't touch anything
        }
    }
}