› How to Connect Gravity Forms to Salesforce Without a Plugin

How to Connect Gravity Forms to Salesforce Without a Plugin

Use Salesforce's Web-to-Lead feature to capture leads from Gravity Forms submissions

Email this to someoneShare on Facebook0Share on Google+0Share on LinkedIn0Share on Tumblr0Share on StumbleUpon0Tweet about this on TwitterShare on Reddit0Pin on Pinterest0

There is a Gravity Forms add-on plugin that connects Gravity Forms and the Salesforce CRM, but the connection can be made without using a plugin.

Advanced users should proceed to Part III – Integrating Gravity Forms and Salesforce.

Part I. – Introduction to Salesforce Lead Fields

To start, you’ll want to set up your Salesforce Lead form fields that will ultimately correspond to the submitted on-site Gravity Form.

Handle this by signing into your Salesforce dashboard and accessing the Build->Leads->Fields section within the Setup menu.

Tip: Salesforce’s Web-to-Lead feature should be enabled by default, but you may want to double-check your settings by visiting Setup->Customize->Leads->Web to Lead.

Here you’ll see a list of the default fields along with their associated data types:

gf-salesforce1

For the sake of simplicity, I’ll be going through the process of connecting a very basic set of fields — such as the form submitter’s first and last name, phone number and company name — between Gravity Forms (GF) and Salesforce (these form fields are active by default within Salesforce, and follow a basic naming convention: first_name, last_name, company_name, etc.). Other than that, we’ll add a new field to demonstrate the process.

In the example, the new field will be a message text area, but feel free to customize to suit your needs.

Click on New to display the form field editor. Since we’re using a text area for the example, select Text Area-Long as the data type, and then proceed to the next panel.

gf-salesforce2

Now, pay attention to this part. On field details panel, you’ll choose the field’s label as well as its ID. For custom form fields, you’ll use the API Name (for example, the API name of our new field is message__c).

gf-salesforce3

Create and save this field before moving on to the next part.

Part II. – Setting Up the Gravity Forms Fields

Now that every field we want to use is set up within Salesforce, we need to create associated fields and the Gravity Form we want to push to Salesforce.

Create your new form in Gravity Forms, and make sure you add each of the fields you want to port to Salesforce:

gf-salesforce6

Now, note the ID of each field, which is what you’ll use to connect the two. You can view a GF form field’s ID by hovering over it within the editor. Schematically, the ID of the fields is determined by the order the fields are added to the form editor.

Notice that we’re using two single-text fields for the name: one for the first name and another for the last — rather than using the Advanced->Name field. This is for the sake of simplicity in the example, as using the Advanced->Name field complicates the porting of the data to Salesforce. If you finish the process and notice that the name comes through as Not Provided in Salesforce, check to make sure you’re using two individual single-text fields for the name input.

In our example, the GF form fields have the following IDs:

Field Name Field ID
First Name 1
Last Name 2
Phone 3
Email 4
Company 5
Message 6

Once your form is published, and you’ve noted the field IDs, you can move on to the next section.

Part III. – Integrating Gravity Forms and Salesforce

Now that we’re square on the GF and Salesforce side, we’ll be turning to WordPress to create a function that sends data from the particular form to Salesforce. This can either be added to your theme’s functions.php file or a plugin header can be added to turn the code into a usable plugin.

<?php
// sends gf form submission data to salesforce
function edwardr_gf_to_salesforce() {
}

Now we’ll connect the various GF form fields with their respective counterparts within Salesforce:

<?php
// sends gf form submission data to salesforce
function edwardr_gf_to_salesforce() {
	$post['first_name'] = $_POST['input_1'];
	$post['last_name'] = $_POST['input_2'];
	$post['phone'] = $_POST['input_3'];
	$post['email'] = $_POST['input_4'];
	$post['company'] = $_POST['input_5'];
	$post['message__c'] = $_POST['input_6'];
	$post['oid'] = 	'XXXXXXXXXXXXXX';
}

Review the above code and make sure you understand it. What we’re doing is using the superglobal $_POST variable to access the submitted form data, which then stores it in WP’s global $post variable for submission to Salesforce. The Gravity Form field input_1 corresponds to the Salesforce first_name field, while input_2 corresponds to the Salesforce last_name field, and so on and so forth.

The Salesforce OID is the object identifier which lets Salesforce know the submission is associated with our account. This part is crucial – do not overlook it, or the data will not be stored in Salesforce. You can retrieve your OID in Salesforce by navigating to Setup->Administration Setup->Company Profile->Company Information.

Next, we’re going to call wp_remote_post to send the data to Salesforce upon submission. This is a handy function that uses the HTTP POST method.

<?php
// sends gf form submission data to salesforce
function edwardr_gf_to_salesforce() {
	$post['first_name'] = $_POST['input_1'];
	$post['last_name'] = $_POST['input_2'];
	$post['phone'] = $_POST['input_3'];
	$post['email'] = $_POST['input_4'];
	$post['company'] = $_POST['input_5'];
	$post['message__c'] = $_POST['input_6'];
	$post['oid'] = 	'XXXXXXXXXXXXXX';

	$args = array( 'body' =&gt; $post );
	$result = wp_remote_post('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', $args);
}

Now, we need to do something with our function, since it’s pretty useless on its own. So we’ll hook into the gform_after_submission action hook. But this action hook applies to all Gravity Forms submissions, so we’ll want to append the form ID to the end to make sure it’s only executed when our specific form is submitted. So make sure you replace FORMID with the ID of your specific form.

<?php
// sends gf form submission data to salesforce
function edwardr_gf_to_salesforce() {
	$post['first_name'] = $_POST['input_1'];
	$post['last_name'] = $_POST['input_2'];
	$post['phone'] = $_POST['input_3'];
	$post['email'] = $_POST['input_4'];
	$post['company'] = $_POST['input_5'];
	$post['message__c'] = $_POST['input_6'];
// the oid is crucial to connect to your saleforce account
	$post['oid'] = 	'XXXXXXXXXXXXXX';

	$args = array( 'body' =&gt; $post );
	$result = wp_remote_post('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', $args);
}
add_action('gform_post_submission_FORMID', 'edwardr_gf_to_salesforce');

And that’s mostly it. Your submitted GF form data should now be ported into Salesforce as new leads.

Part IV. – Wrapping it Up as a Plugin

There’s nothing wrong with dropping the function into your theme’s functions.php file, but for better compartmentalization, you may want to use this code as a custom plugin.

If so, it’s only necessary to add the relevant plugin header information, as seen below:

<?php
/**
 * Plugin Name: Gravity Forms Data to Salesforce
 * Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
 * Description: A brief description of the Plugin.
 * Version: The Plugin's Version Number, e.g.: 1.0
 * Author: Name Of The Plugin Author
 * Author URI: http://URI_Of_The_Plugin_Author
 * License: A &quot;Slug&quot; license name e.g. GPL2
 */
 
// sends gf form submission data to salesforce
function edwardr_gf_to_salesforce() {
	$post['first_name'] = $_POST['input_1'];
	$post['last_name'] = $_POST['input_2'];
	$post['phone'] = $_POST['input_3'];
	$post['email'] = $_POST['input_4'];
	$post['company'] = $_POST['input_5'];
	$post['message__c'] = $_POST['input_6'];
	// the oid is crucial to connect to your salesforce account
	$post['oid'] = 	'XXXXXXXXXXXXXX';

	$args = array( 'body' =&gt; $post );
	$result = wp_remote_post('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', $args);
}
add_action('gform_post_submission_FORMID', 'edwardr_gf_to_salesforce');

Customize the code to match your forms and Salesforce account, and then save this code in a PHP file (e.g. – gf-salesforce-port.php) and upload it to your /wp-content/plugins directory, and then activate via the Plugins administration panel.

Continue reading for debugging and advanced troubleshooting.

Part V. – Debugging and Troubleshooting

If you’re having trouble pushing your data from GF to Salesforce, you may find SF’s debug mode helpful. To enable debug mode for your submission, add the following to the function that sends the data to Salesforce:

<?php
// sets debug mode to true
$post['debug'] = '1';
// configures the debug email
$post['debugEmail'] = 'your-email-address';

Be sure to replace your-email-address with your custom email address. This feature fires off an email to the inputted address with the details of the submission, and may be helpful for diagnosing problems.

Common problems with this setup include some fields coming through as [NotProvided]. The most likely culprit is an incorrect ID in your GF-to-Salesforce setup, or an incorrect data type. Submitted data types must be consistent. For example, if a certain Salesforce field is a picklist, you’ll have problems if you try to push through a string from a text field.

Other issues could come from wp_remote_post. If you’re using a self-signed SSL certificate, you may need to explicity set the add the sslverify parameter to false, as seen below:

<?php
$args = array( 'body' =&gt; $post, 'sslverify' =&gt; false, );
$result = wp_remote_post('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', $args);

Thanks for reading. If you have a question or problem, please leave a comment below and I will try to help out.

Subscribe for weekly tutorials


  • david

    Hi, Thanks for this tutorial, really helpful.
    One question, we have alot of different forms we’d like to pass to Salesforce, do I need to create a new plugin for each form? They all require email, name, organization and donation, I can make sure they all have the same id, but how would I vary the FORM-ID?
    Thanks for your help
    David

    • edwardrj

      Hi David, if you have several forms, with different fields, the easiest way to do it would be to create a function as seen above for each one, and then hook each into the ‘gform_post_submission_FORMID’ hook based on the form ID.

      • david

        Hi Edward, Thanks for your reply.
        In my case it’s better if I use your plugin solution, I want to keep the field id the same across the forms, so I was wondering if it would be possible to pass the FORM-ID to the plugin or could I use a ‘switch (form-id) case 1: form-id = “in the plugin code?
        Thanks
        David

  • david

    Hi, I’m working on the website of a non-profit organization. We have Salesforce enterprise edition. I know very little about Salesforce, but I’ve set up a connection (Salesforce Feed) between Gravity Forms and Salesforce. Please could you explain how/where I can see the information that was passed to Salesforce from my Gravity forms, my Salesforce object is a Lead. I don’t currently have System Admin rights
    on Salesforce.

    Thanks for you help
    David

  • a guy from montreal

    is it possible to feed contact tab instead of lead ?

  • a guy from montreal

    Sorry.

    Actually I have a problem with the

    $args = array( ‘body’ => $post, ‘sslverify’ => false, );

    it says ”

    Parse error: syntax error, unexpected ‘=’, expecting ‘)’ in /home/mdexe004/public_html/wp-content/plugins/To saleforce/gf-salesforce-port.php on line 51

  • David

    Hi again.

    I just though that ill probably have anotjer problem here.

    How it works for date Values and multiple choices picklist ?

    I tried many ways (like sending data with confirmation/redirection ) but always custom date field and multichoice picklist have problems

    Thank you very much for your help