Integrating Drupal Webforms with Salesforce
Integrating Drupal webforms with the popular CRM tool Salesforce is a common request from our clients. The concept works like this: Visitors fill out a contact webform on the Drupal website. That data is then transmitted to Salesforce, where it would be stored as a new record (or "lead" in CRM parlance.)
Simple right? Well, it used to be. Salesforce does have an API, and there's a Salesforce Suite Drupal module which integrates with it. Better yet, there was a Salesforce Webform Integration module that extended the ability of the Salesforce Suite module, allowing easy and automatic exportation of data from webforms to Salesforce.
Sadly, this solution is no longer available. A note on the Salesforce Suite module page at Drupal.org explains that the "Salesforce Webform module is no longer compatible with the latest version of the Salesforce Suite for Drupal 6.x." A new, compatable version is supposedly being worked on, but progress seems to have slowed on that front. What's more, development of a Drupal 7 version of this module has not even begun.
So are we dead in the water then? Of course not! This is Drupal we're talking about, and with Drupal there is always a custom module solution. In this case, its suprisingly simple.
The process works like this:
- Log into Salesforce and create a Web-to-Lead form.
- Recreate a form with the same fields in Drupal using Webforms.
- Add a hidden field to the webform called 'oid', with a value that matches the hidden oid field that should be present in the Web-to-Lead form you created.
- Create a custom module that implements hook_webform_submission_insert(), posting the webform data to Salesforce the exact same way the Web-to-Lead form does (after the webform data has been validated and stored on the Drupal side.)
Assuming you have already completed the first 3 steps, lets focus on what an implementation of hook_webform_submission_insert might look like in Drupal 7:
<?php
function salesforce_webform_submission_insert($node, $submission) {
$components = $node->webform['components'];
$salesforce_data = array();
// (required) Leave return URL empty.
$salesforce_data['retURL'] = '';
if (
_salesforce_form_id($submission, $components, '00DV00000001Pqe')) {
foreach ($components as $i => $component) {
$salesforce_data['oid'] = '00DV00000001Pqe';
$form_key = $component['form_key'];
$value = _salesforce_webform_value($submission, $component['cid']);
if (
$form_key == 'first_name') {
$salesforce_data['first_name'] = $value;
}
if ($form_key == 'last_name') {
$salesforce_data['last_name'] = $value;
}
if ($form_key == 'email') {
$salesforce_data['email'] = $value;
}
}
// Post data to SalesForce.com
_salesforce_webform_post_data($salesforce_data);
}
}
function
_salesforce_webform_value($submission, $cid) {
$result = "";
$data = $submission->data[$cid]['value'];
if (count($data) > 0) {
$result = join(";", $data);
}
return $result;
}
function
_salesforce_form_id($submission, $components, $form_id) {
$result = FALSE;
foreach ($components as $i => $component) {
$form_key = $component['form_key'];
$value = _salesforce_webform_value($submission, $component['cid']);
if ($form_key == 'oid' && $value == $form_id) {
$result = TRUE;
}
}
return $result;
}
function
_salesforce_webform_post_data($fields) {
$fields['encoding'] = 'UTF-8';
$post_url = "https://cs12.salesforce.com/servlet/servlet.WebToLead?";
$result = new stdClass;
if (count($fields) > 0) {
$data = array();
foreach ($fields as $key => $value) {
$data[] = $key . '=' . $value;
}
$options = array();
$options['headers'] = array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$options['method'] = 'post';
$options['data'] = join('&', $data);
$result = drupal_http_request($post_url, $options);
}
return $result;
}
?>The outermost if-statement in salesforce_webform_submission_insert calls helper function _salesforce_form_id to verify that the hidden 'oid' field is present in the webform, and that it contains the correct value. This is necessary because this hook will be called whenever any webform on your website has been submitted, not necessarily the webform you want to integrate with Salesforce.
Note: In the above code, 00DV00000001Pqe is only an example value for the hidden oid field. you should replace it with the unique oid identifier from the Web-to-Lead Salesforce form you previously generated.
If the if-statement returns TRUE, then salesforce_webform_submission_insert calls helper function _salesforce_webform_post_data to post the data to Salesforce. The function that does the heavy lifting of the actual HTTP request is called in this line:
$result = drupal_http_request($post_url, $options);
More information about drupal_http_request() can be found in the Drupal API documentation.
That's it! Slightly more complicated than the old Salesforce Suite/Salesforce Webform Integration module approach, but this one works now, no waiting required. This approach could also be easily adapted to handle more than one webform on the site being posted to different Salesforce objects, or even completely different Salesforce accounts.
11 comments
Just in time!
Thanks, Chris! This was very helpful, as well as timely because we just joined Salesforce.com this week. I created our module, tested it thoroughly, and now both of our webforms will be sending leads to Salesforce.com. -Kevin @Quevin
$post_url change to mention
BTW, I did need to change my $post_url for this to work for me:
$post_url = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8";
Salesforce Integration
This is really useful. If you figure out an easy way to allow non-technical users to be able to change/select which Salesforce table they are posting to from the website, please share how you did it.
Fruta
Thanks - very helpful
We ran up against this issue just the other day, and were stalled waiting for a good solution. Bang! This is perfect. Thanks!
Steve R.
There's also http://drupal
There's also http://drupal.org/project/salesforcewebform that handles very basic integration.
Thanks for this info. A very
Thanks for this info. A very help full article. Now I can integrate these both with no error.
thanks
Thank you very much for
Thank you very much for sharing. Definitely solved my problem.
Will this solution work in Drupal 6?
I was planning to learn the forms API to do exactly what was done here but by extending the salesforce suite. I'm new to Drupal and am looking for a shortcut if at all possible, but would like a production ready solution.
i created the web to lead
I created the web to lead form copied the oid number and put it on a hidden oid field as value also named the fields the same on Drupal and Salesforce and I think i did everything rigth until step 4. I'm not really sure what happended but I created webform.php copied and pated the functions and placed it inside my Drupal 7 theme also i added
$form_key == ' ') {
$salesforce_data[' '] = $value;
}
to all the fields I have and changed the oid is there something I did wrong?
Great post
Fabulous samples. I had no problem making this work for my project. Thanks a million.
Great info - here's another D7 Module
Chris - I came across your post while I was searching for Salesforce solutions for Drupal 7. Great post and thanks for the info. I also stumbled across The Salesforce Web-to-Lead module, which is in RC3 right now. Basically does the same thing, but allows you to map fields as well as input your OID. (http://drupal.org/project/sfweb2lead_webform). May help those looking for a module based OTB solution. Fairly easy to configure and get working.
Post new comment