Javascript to autofill a form drop down - displayed versus stored value | Community
Skip to main content
Taylor_McCarty
Level 3
September 14, 2020
Solved

Javascript to autofill a form drop down - displayed versus stored value

  • September 14, 2020
  • 1 reply
  • 5268 views

So I have been asked by our Marketing VP to see about auto-filling an interested product field on our forms based upon the page the user is currently on. The idea being to limit the number of fields they have to actually fill something out. I know this is possible, but the question I do have is if the Javascript can be set up such that the form is autofilled with the displayed name of the product, but still actually capture the stored value upon form submission. 

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

So the URL structure would be something like /programs/program1

 

Then was hoping the javascript could be written such that if URL contains programs/program1 then populate the program field on the form with program1, but again having the value that is stored upon submission be the 18 digit SFDC number for program 1.

 

Again this may not be a good idea or it may violate a best practice, just trying to do my due diligence so I can accurate report by to the VP


It's totally doable and I wouldn't even consider it a bad practice — the URL just needs to be predictably structured.

 

You don't want to be doing "fuzzy searches" all over the URL for an occurence of something that seems a like a program, it should be in a specific place. Like in /programs/program1 you can abstract that to the URI Template /program/{programName}.

1 reply

SanfordWhiteman
Level 10
September 14, 2020

but the question I do have is if the Javascript can be set up such that the form is autofilled with the displayed name of the product, but still actually capture the stored value upon form submission. 

Not sure exactly what you mean here.

 

Are you distinguishing between a display name (friendly name) and some other more technical value to be stored on the back end? If so, you can't really use a single field for this (there's a crazy hack for it but let's ignore it for the moment) but 2 fields, one displayed and one hidden, will work.

Taylor_McCarty
Level 3
September 16, 2020

So on the form we have programs listed with a friendly name but the value that is actually captured and stored when the record created is an 18 digit number that relates to our SFDC instance. So I was wondering if the script could populate the field with the friendly name, but when the form is submitted it still passes over the 18 digit SFDC number, not the friendly name.

SanfordWhiteman
Level 10
September 16, 2020

When you autofill a field (from URL query params, etc.) it's based on the server value (that is, the alphanumeric SFDC ID).

 

If you want to select the <option> that has value="ABC0123" then you pass program=ABC0123 in the URL.

 

But if the field is a <select>, it will still show the display name of the corresponding option (like "My Program" or whatever you have). It will send "ABC0123" to the server, since that's the value of the option. It won't create a new <option> whose friendly name and server value are both "ABC0123". So I don't see yet how the default behavior departs from what you describe.