Triggering form prefills when conditional logic is used? | Community
Skip to main content
Casey_Grimes2
Level 10
July 18, 2017
Question

Triggering form prefills when conditional logic is used?

  • July 18, 2017
  • 2 replies
  • 4130 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

SanfordWhiteman
Level 10
July 18, 2017

Will provide code later today for this!

SanfordWhiteman
Level 10
July 19, 2017

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);

});

Casey_Grimes2
Level 10
July 19, 2017

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.

SanfordWhiteman
Level 10
July 19, 2017

Now it seems like we're talking about something else again.

Toset up conditional selects, you always need to have all the possible options in the master list (the option list associated with the form field itself, not the option list/s in the Visibility Rules dialog). Once you have all the options there, then you can use subsets of the options in VRs. You don't need JS. Hope that helps for that part of the picture.