Using an Address Field Dynamically to sync to Salesforce

By: Amy Dalrymple | Asked: 07/11/2022
ForumsCategory: Code HelpUsing an Address Field Dynamically to sync to Salesforce
Amy Dalrymple asked 2 years ago
Hello, I am using the new Geolocation add on for a lead form. I have an Address field to capture Street, Suite Number, City, State, and ZIP. I need to sync these values individually into Salesforce. I found the documentation for pulling each element of the Address field into a post-submit view or email:
[x show="line1"]
(https://formidableforms.com/knowledgebase/address/#kb-display-address-fields) But is there any way to dynamically populate hidden fields with each of the elements individually? I am experimenting with a JavaScript solution now but not having much luck. Thanks!
Wilco Wietsma replied 2 years ago

If you're going to populate hidden (individual) fields anyway, wouldn't it be easier to just use those fields instead of the "adress" field in Formidable? Also

However thanks for the javascript. Might be usefull sometime 🙂

Amy Dalrymple replied 2 years ago

@Wilco Wietsma - I can't use the address Geolocation on regular fields unfortunately. That would've been much easier, for sure!

Victor Font replied 2 years ago

What do you mean by that Amy? I was using the Google Maps API on regular fields long before there was a Geolocation add-on. Personally, I have never "played" with Geo add-on, but I doubt I would use it on one of my personal projects. I find it easier just to write the code myself than to learn another tool.

Amy Dalrymple replied 2 years ago

@Victor Font - I have a couple goals in mind with this client-driven project:

1 - On the front end of the site for users, I want them to be able to enter part of their address and then be able to use the Geo Add-On to dynamically populate the other parts of ther address. It's a much nicer experience than using several individual fields to collect pieces of an address.

2 - On the back end of the site for admins, I needed to be able to pull out just the street address, just the ZIP code, etc. in order to accurately sync parts of the address to various Salesforce fields. This is another reason why I needed to use the multi-field group of "Address" instead of the single line Address field.

So I did mis-type early: the Geo Add-On may work on "regular" fields but so far I haven't figured out how to group them to all dynamically populate together.

If I wasn't using Formidable or a third-party plugin, I would for sure write my own code - but in favor of efficiency and reducing possible errors, I look to Formidable and its Add Ons to do the heavy lifting.

1 Answers
Best Answer
Amy Dalrymple answered 2 years ago
Well, I was able to solve this with JS. Here is my snippet, in case it can help anyone else!
<script type="text/javascript">
    var restaurantStreet = document.getElementById('field_tjadd_line1').value;
    var restaurantSuite = document.getElementById('field_tjadd_line2').value;
    var restaurantCity = document.getElementById('field_tjadd_city').value;
    var restaurantState = document.getElementById('field_tjadd_state').value;
    var restaurantZip = document.getElementById('field_tjadd_zip').value;

    document.getElementById('field_tjadd_line1').addEventListener('change', function() {
        doChange();
    });

    document.getElementById('field_tjadd_line2').addEventListener('change', function() {
        doChange();
    });

    document.getElementById('field_tjadd_city').addEventListener('change', function() {
        doChange();
    });

    document.getElementById('field_tjadd_state').addEventListener('change', function() {
        doChange();
    });

    document.getElementById('field_tjadd_zip').addEventListener('change', function() {
        doChange();
    });

    //ONE FUNCTION TO RULE THEM ALL
    function doChange() {
        restaurantStreet = document.getElementById('field_tjadd_line1').value;
        document.getElementById("field_rf041").value = restaurantStreet;

        restaurantSuite = document.getElementById('field_tjadd_line2').value;
        document.getElementById("field_rf041").value = restaurantStreet + ' ' + restaurantSuite;

        restaurantCity = document.getElementById('field_tjadd_city').value;
        document.getElementById("field_2n1uj").value = restaurantCity;

        restaurantState = document.getElementById('field_tjadd_state').value;
        document.getElementById("field_5g8u").value = restaurantState;

        restaurantZip = document.getElementById('field_tjadd_zip').value;
        document.getElementById("field_symjs").value = restaurantZip;
    }
</script>
Bobby Clapp replied 2 years ago

Thanks for sharing!

Michael ClarkMichael Clark replied 2 years ago

That's beautiful. Wonderfully generous share, as well. Thanks!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right