• Resolved mrcairney

    (@mrcairney)


    Hi, I know that I can use filters for this, but I’m not sure just how.

    I would like to capture URL parameters in the form. I use this for PPC tracking form entires.

    An example URL would look like this:

    https://www.example.com?c=123&kw=string

    the ‘c’ and ‘kw’ fields are present in SF, and I can reference them either by id (00N20000009J06l) or API name (Campaign_ID__c)

    So my question is if I create these above as hidden fields in the form, what syntax do I need for the filter? At the moment, I think I need to add a little more than just the below as otherwise, the filter doesn’t know which parameter is which.

    So I need to say that if parameter ‘c’ exists in the URL it is field campaign_ID__c

    <?php
    /*
    How to use:
    1. Create a custom URL field at SalesForce
    2. Replace URL_CUSTOM_FIELD_NAME below with the name of the custom field you setup in SalesForce, 
       it will be something like EmbedUrl__c
    3. Add a hidden field to each form with the same field name (e.g. "EmbedUrl__c").
    4. Make sure the hidden field is enabled (or it won’t be output with the form/get filled/be sent to SF).
    5. Profit
    */
    add_filter( 'salesforce_w2l_field_value', 'salesforce_w2l_field_c', 10, 3 );
    function salesforce_w2l_field_c( $val, $field, $form ){
        // Target a specific field on all forms
        if( $field == 'CAMPAIGN_ID__c' ) 
             $val = esc_url("https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        return $val;
        
    }

    Should the above work?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter mrcairney

    (@mrcairney)

    I was looking at the wrong filter here.

    What I actually need to do is like below. Do I have to make a function for each parameter, or can I add a bunch into the one? If I can, what would the syntax be?

    add_filter( 'salesforce_w2l_field_value', 'salesforce_w2l_field_value_querystring_example', 10, 3 );
    
    function salesforce_w2l_field_value_querystring_example( $val, $field, $form ){
    
        $form_id = 1; // form id to act upon
        $field_name = 'source__c'; // API Name of the field you want to autofill
        $qs_var = 'source'; // e.g. ?source=foo
    
        if( $form == $form_id && $field_name == $field ){
            if( isset( $_GET[ $qs_var ] ) ){
                return $_GET[ $qs_var ];
            }
        }
    
        return $val;
    
    }

    This solution worked for me:

    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    $(document).ready(function(){
        $("#sf_field1").val(getParameterByName("param1"));
        $("#sf_field2").val(getParameterByName("param2"));
    });

    Then https://www.example.com/page/?param1=123&param2=XYZ will populate the fields for you

    Good Luck!

    Thread Starter mrcairney

    (@mrcairney)

    Hi jdevelop, I actually managed to fix this using the filter as per my last post, but I like how you think!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘URL Parameter help’ is closed to new replies.