Field dependancy : display a textfield dependant from a custom multi-enum

Hello there !

i'm building a new project under Sugar Pro 7.5.2 and i'm meeting some difficulties : 

What i have and what i would like :

I have created a muliple choices dropdown, and a textfield.
I would like to display the textfield only if the user choses a specific value of my multi-enum dropdown.

So normally i would do that through studio, with the dependancy formula.


What i tried and what failed :

But there is a problem : my multi choice dpdwn is not showing under the Fields column in the formula editor. And even if i try to add it manually in my formula, i have an error of "field unrecognized".

So, how can i display a field on a Multi select field dependancy ?

I did not find any tips or tricks in my Community search, i even found martin peet being unanswered :
Using a Multi select box as dependancey criteria

it's been one year !

Please, tell me if i'll have to do that by code, and how.

Have a nice day ! :)
  • Hi Gaelle,

    1- Could you show us create-actions content file?
    2- You have to put into initialize() function this code:
    this.on("render", this.<function_name>, this);
    Best Regards,
    Rubén Recacha.
    Consultant REDK Software Engineering
  • THANKS SO MUCH @Ruben !
    By putting the render, and by copying my code in create-actions.js, i finally did it ! My final code  in the next response :)
  • WELL !
    We got the solution here !

    This is the end, i have the solution to all my questions about how display/hide a textfield dependantly from a multi-select field !

    And that is Thanks to @Ruben Recacha, @Rolustech and @EvilPery !

    In custom/modules/YOURMODULE/clients/base/views/record/record.js :

    ({    extendsFrom: 'RecordView',
        initialize: function(options) {
      this._super('initialize', [options]);
      this.on("render", this.displayFunction, this);//launch it whenever your page is rendered
      this.model.on("change:YourMultiSelect",this.displayFunction, this);//display changes when the multi select change
        },
    displayFunctiondisplayDetailsScoring:function(){
      if (_.indexOf(this.model.get('YourMultiSelect'), "others") === -1){
       $('[data-name="YourTextField"]').hide();
      }else{
       $('[data-name="YourTextField"]').show();
      }
    },
    })

    To do it on the create page, you just have to copy/paste this code to custom/modules/YOURMODULE/clients/base/views/create-actions/create-actions.js

    and replace
    ({    extendsFrom: 'RecordView',

    by :
    ({    extendsFrom: 'CreateActionsView',

    And that is all ! :)
    So, thanks again to @Rolustech, @EvilPeri and @Ruben, they give me clues, they give me information, they rocks :)
  • Hi GaelleFernandez,

    This is just a tip for when you are hiding elements. It is better to unset the value as you hide the field so that when you save any lingering values are not kept.

    so in your case 
    $('[data-name="YourTextField"]').hide();
    this.model.set('YourTextField',null); //just add this line, it can be null, zero, blank whichever you want just unset the value.

    delete this.model.attributes['YourTextField'] //this is optional and you can add this if you really want to remove the key value pair
  • Sorry if it seems out of place , thought to keep the discussion linked since I'm building on this.

    Anyone figured out how to style things as closely as possible to sugarcrm default behaviour ( dependent field ) . for .show and hide ?

    Tried multiple things such as addClass("hide") instead of .hide and removeClass("hide").

    , searched the style guide but couldn't find it. Basically the rows will collapse and the other field will take full width if there's more than 1 on the same row and one of them shows as hidden. This doesn't happen on dependent fields. Recordit: Record screencasts fast & free! with GIF Support!

    UPDATE

    It seems to be the vis_action_hidden class so my code now looks something like : However can't get that pink coloured fade-in effect.

                if(this.model.get('objectifs_c').contains("12") ) { //programation action commerciale
                    $('[data-name="v_liste_actionco"]').removeClass("vis_action_hidden");
                }
    
    
                else {
                    $('[data-name="v_liste_actionco"]').addClass("vis_action_hidden");
                }
    
  • I worked on exactly the same issue in Sugar 9.2. We have a multi-select field in the Meetings module where one of the choices is "Other". A text field had to be displayed when "Other" is one of the selections. First, I set it up as shown by Gaelle Fernandez, using the ".hide" and ".show" methods. This worked to some degree, but not well enough. When the multiselect options were initially entered, the text field would not open when "Other" was selected. Instead, the text field would show a message on mouse-over: "This field is locked because it is involved in a running process" (see image). But, if I saved the meeting and then edited it, the text field would open when "Other" was selected, as expected. When I replaced ".hide" and ".show" with ".removeClass" and ".addClass", as shown by Caim Astraea, everything started to work as expected. Thank you all!

  • I am new to sugarCRM. Recently I have searched for the same solution and google drop me here. I tried the above solutions but they are working for only dropdown field. Since, sugar logic is not working for multiselect field dependency. Multiselect field dependency keeps values as Objects. SO, by converting it into array and iterating the array we can find whether the multiselect field contains our required value or not.

    I know this this is very late but this will be helpful for the future readers like me.

    //solutions_c is Multiselect field
    //revenue_c is Textfield
    
    initialize: function(options) {
        this.plugins = _.union(this.plugins || [], ['HistoricalSummary']);
        this._super('initialize', [options]);
    
        //declare onChange
        this.model.on("change:solutions_c", this.SolutionsOnChange, this);
    },
    
    SolutionsOnChange: function(){
        var solutionsField = this.model.get('solutions_c').toString();
        var flag = 0; //set flag as 0
        //exploid by (,)
        var solutionsArray = solutionsField.split(",");
        for(var i = 0; i < solutionsArray.length; i++ ){
            if(solutionsArray[i] == "Electronics"){
                flag = 1; //set flag as 1
            }
        }
        if(flag == 1){
            $('[data-name="revenue_c"]').css('visibility', 'visible');
        }
        else{
            $('[data-name="revenue_c"]').css('visibility', 'hidden'); 
        }
    },



    Thank you,