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 ! :)
Parents
  • I would do like this: in your editviewdefs file, include in you field the follow: 
    'displayParams' => 
    array (  
          'field' => 
          array (
             'onchange' => 'someFunction()',
          ),

    In this function you may programming via java script the code to handle the hide/display logic.
  • Hi,

    In your record view controller, you can handle this scenario using the following code:

        extendsFrom: 'RecordView',
        initialize: function(options) {
            this.plugins = _.union(this.plugins || [], ['HistoricalSummary']);
            this._super('initialize', [options]);
      this.model.on("change:is_show_c",this.CustomOnChange, this);
        },
    CustomOnChange: function(){
      var is_show_c1=this.model.get('is_show_c');
      if (is_show_c1 == "No")
        $('[data-name="testfield_c"]').hide();
      else
        $('[data-name="testfield_c"]').show();
       },


    // is_show_c is the multi select field
    // testfield_c is the text field 
    Rolustech: SugarCRM Engineering
    Website: www.rolustech.com
    Ph (US): +1 - 310 - 492 - 5564
    Ph (UK): +44 - 207 - 9938 - 524
    Email: info@rolustech.com
    Skype: rolustech
  • Well, first of all, Thanks a lot @Rolustech for your patience, and yes, i had run a Quick Repair (that is the first thing you learn about Sugar, haha !), and thanks a lot, you put me on the right tracks !

    @EvilPeri, thanks a lot too, I did not know how worked the multi-select field, and that was the first question i was wondering myself. You put me on the right tracks too !
    Because you have some knowledge on multi-select, do you know wich part of the list is in the array/object ? i mean, are they the value or the labels ?

    And yes @Maira Jessy, I love to discuss about Sugar like this, i always learn new tricks and improve my code with such people, who has the will to share their knowledges :)

    For the moment, i haven't got the total solution, but i will digg some more, and i'm sure it is not that far away :)
Reply
  • Well, first of all, Thanks a lot @Rolustech for your patience, and yes, i had run a Quick Repair (that is the first thing you learn about Sugar, haha !), and thanks a lot, you put me on the right tracks !

    @EvilPeri, thanks a lot too, I did not know how worked the multi-select field, and that was the first question i was wondering myself. You put me on the right tracks too !
    Because you have some knowledge on multi-select, do you know wich part of the list is in the array/object ? i mean, are they the value or the labels ?

    And yes @Maira Jessy, I love to discuss about Sugar like this, i always learn new tricks and improve my code with such people, who has the will to share their knowledges :)

    For the moment, i haven't got the total solution, but i will digg some more, and i'm sure it is not that far away :)
Children
  • The one stored on your model is the values the labels can be accessed via 

    app.metadata._dev_data.app_list_strings['<your listname here>']
  • thanks a lot @EvilPeri, that 's really good to know :)
    Another question : i want to hide/show the textfield everytime the multi-select changes. It don't seem to be the case currently. Do i have to do some ajax ?
  • Thanks a lot again @EvilPeri, that is a good thing to know :)
  • actually Rolustech's code snippet above works fine when it is first displayed but i think moving the event listener to the bindDataChange method, or as a last resort if that still doesnt work then what you could do is add the event listener to the _render/render method
  • Thanks to both of you @EvilPeri and @Rolsutech, i've made it work, but i still have some questions :
    Here is the code i've wrote in record.js :

    ({    extendsFrom: 'RecordView',
        initialize: function(options) {
      this._super('initialize', [options]);
      /*OnChange on multi-select fields for scoring*/
      this.model.on("change:scoring_2_roues_c",this.Change2Roues, this);
        },
    Change2Roues: function(){  if (_.indexOf(this.model.get('scoring_2_roues_c'), "sautr") == -1){
    $('[data-name="detail_scoring_2_roues_c"]').hide(); //Autres n'est pas sélectionné
    }else{
    $('[data-name="detail_scoring_2_roues_c"]').show(); //Autres est sélectionné
    }
       },
    }) 

    It works great.
    Now here are my 2 questions :
    - It works only when the user is editing a Lead (which has already been created). I have tried to add this code in custom/modules/Leads/clients/base/views/create-actions/create-actions.js, but it does not load when a user is creating a lead...
    Do you have any idea on how to achieve this ?
    - As you can see, the event is the "onchange" on scoring_2_roues_c, but i would like the detail_scoring_2_roues_c to hide (still depending upon scoring_2_roues_c 's values) when the page is launched, so i will have to write some code as the page launch. Which event do i have to use ? How should i call the function ?

    Again, thanks a lot to you, because with your help, i'm making huge improvement !
  • 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");
                }