8 Replies Latest reply on Jul 19, 2017 12:30 PM by Courtney Grimes

    Triggering form prefills when conditional logic is used?

    Courtney Grimes

      So, let's say I have a Marketo form with a picklist asking about fruit.

       

      Fruit: Apple, Oranges, Bananas

       

      This triggers a conditional picklist based on Fruit to show Fruit Type values--if you pick Bananas, you get a list with Cavendish, Plantain and if you pick Orange you get Navel, Clementine.

       

      Because Marketo uses .mktoPlaceholder on conditional picklists rather than generating the select field, you can't prefill the Fruit Type field--and moreover, if you prefill the Fruit field, this will not trigger the script to change from .mktoPlaceholder to the proper Fruit Type!

       

      Is there any way around this at this time? Has anyone solved this problem?

        • Re: Triggering form prefills when conditional logic is used?
          Sanford Whiteman

          Will provide code later today for this!

          • Re: Triggering form prefills when conditional logic is used?
            Sanford Whiteman

            So I found and developed a fix for a bug, but it seems like the opposite of what you're describing. I think we need to examine this more.

             

            I find that I can PreFill a field that is only visible based on matching Visibility Rules. In your example, if Fruit Type is 'Clementine', the Fruit Type dropdown will have the stored value 'Clementine' whenever it becomes visible, i.e. whenever Fruit is 'Orange' (whether Fruit is itself set via PreFill or manually).

             

            The bug I see is that I can't un-PreFill, if you will, the Fruit Type even when the currently selected Fruit doesn't contain the stored Fruit Type as an option. So if 'Orange' and 'Clementine' and stored and I manually change the Fruit to Banana, the Fruit Type will be null (meaning there will be no value in the dropdown, as is the case whenever you try to set a value that's currently not valid).  Since in this case you'd want the dropdown to have the placeholder 'Select...' selected, you can fix it with this code, which changes the nulls to empty strings so 'Select...' is selected when necessary:

             

            MktoForms2.whenRendered(function(form){
              var nullToBlankFields = ['Fruit_Type__c'];
            
              /* ---- NO NEED TO EDIT BELOW THIS LINE ---- */
            
              var currentVals = form.getValues();
                  mktoFieldsObj = {};
            
              nullToBlankFields
              .filter(function(field){
                return currentVals[field] === null;
              })
              .forEach(function(field){
                mktoFieldsObj[field] = "";
              });
            
              form.setValues(mktoFieldsObj);
            });
            
              • Re: Triggering form prefills when conditional logic is used?
                Courtney Grimes

                Sanford Whiteman wrote:

                 

                The bug I see is that I can't un-PreFill, if you will, the Fruit Type even when the currently selected Fruit doesn't contain the stored Fruit Type as an option. So if 'Orange' and 'Clementine' and stored and I manually change the Fruit to Banana, the Fruit Type will be null (meaning there will be no value in the dropdown, as is the case whenever you try to set a value that's currently not valid).

                I thought this was a known bug with Forms 2.0, but maybe not--even in vanilla instances, if you change the parent conditional field, Marketo can't generate the proper child conditional field (or even re-render the field!) It's something that's driven me crazy for a while.

                 

                It's frustrating, because as far as I can tell, the only paths forward are to make multiple Fruit Type fields and control the conditional logic manually or always expose all Fruit Type values and use JavaScript to remove items selectively. Neither is ideal and it's a little disappointing Marketo doesn't support what should be out-of-the-box functionality around conditional fields.