How To Integrate Salesforce With Cforms (and have multiple Salesforce Web to Lead forms in WordPress)

If you use Salesforce and you have a WordPress powered site you may be frustrated. Salesforce is a wonderful tool for managing your customers and potential leads. WordPress is the best CMS out there today (I am a little biased, but it’s true). So why can’t they play together?

There IS a solution out there now… The WordPress-to-lead for Salesforce CRM Plugin by Yoast is a noble effort. It’s easy to use, which is more than I can say for the instructions I will be giving below (but even if you’re not a coder, you should be able to follow). The problem is that it only offers one form.

But what if you are running multiple campaigns? What if you need different forms? You can use the Web to Lead custom forms that Salesforce offers, but if you want to have required fields, you’ll need to code special JavaScript to validate those forms… That’s not a very good solution either.

Also, what if you want to keep a backup of the leads. The Web to Lead forms have a limit of 500 leads a day. While they assure you that you will be emailed the rest of the leads so you can input them yourself, you may want a central place to track your leads aside from Salesforce.

This is where Cforms II comes in. Cforms is my favorite WordPress forms plugin. It offers a beautiful GUI backend, many features, themes and even form tracking. Wouldn’t it be great if you could combine the best WordPress form plugin with the best CRM in town?

Note:

Once you make these adaptations you should be aware that EVERY form you create with Cforms on your site will try to submit the information to Salesforce. If you would like only SOME of your forms to be submitted see the appendix at the end of the post.

Step 1 – Hack Cforms

I often hack plugins. It is laborious, trying to figure out the inner workings of certain plugins. They tend to be poorly commented and many best programming practices are avoided. In contrast, Cforms is beautiful. Not only is it understandable as code; but the author created a file: “my-functions.php” whose sole purpose is to allow other coders to add functionality to the plugin without having to hack. This is what we will be “hacking”.

Find the following block of code:

 

And replace it with this:

 

Explanation:

Initially, I thought that the “Send form data to an alternative page” option under “Core Form Admin/Email Options” but whatever I did, the forms, when submitted, got stuck on the dreaded “One moment please…” Ajax message. The answer was submission via cURL (ask your neighborhood coder to explain). In addition there is some data processing involved so that the form contents will be understood by Salesforce.

Step 2 – The Form

The next stage is building your forms in Cforms.

Click to enlarge

I am assuming that you’ve already built some web-to-lead forms in Salesforce, and you like the idea of having a form engine running them with form validation and database tracking. If you haven’t yet, please create a form in Salesforce. It is not easy to figure out how to do this, and the salesforce help sometimes omits important information, not to mention needing an account to access the help. So getting the code for a web-to-lead form warrants a post of it’s own, but that will have to be for another time.

Once, you have the code for the form go through it and add to your Cforms form a field for each field that you have in the Salesforce form (if you add extra fields I don’t know if Salesforce will process that information).

One thing to keep in mind when doing this is that you should make sure to name the fields EXACTLY as they do. Each field in the Salesforce code begins with a

For example:

<label for="company">Company</label><input id="company" maxlength="40" name="company" size="20" type="text" /><br>
<label for="first_name">First Name</label><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>

So you want to call your first Cforms field “Company” and your next one “First Name”.

When you’re done adding all the fields there are a few more “hidden” fields that you will need to add:

  1. In position #1 add a field.
  2. Under “Type” change the field from “single line of text” to “hidden field”.
  3. In the “Field Name” type ‘oid|’ and  then without spaces your Salesforce organization ID number.

To find your oid refer back to your form code provided by Salesforce. About 10-15 lines down you should see two lines like this:

<form action="<a href="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8">https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8</a>" method="POST">
<input type=hidden name="oid" value="XXXXXXXXXXXXXXX">

Where instead of the letter X you’ll see a string of numbers and letters (I’ve counted 15 numbers, it may vary, but probably not). That’s your Salesforce organization ID.

If you are running campaigns and you want the form to be added to one of those campaigns, you will need to add the Campaign ID as well as a hidden value.

In your Salesforce code look for something like this:

	<label for="Campaign_ID">Campaign</label>
	<select  id="Campaign_ID" name="Campaign_ID">
		<option value="">--None--</option>
		<option value="XXXXXXXXXXXXXXX">Contact us - Website</option>
	</select>

There probably will be more <option> tags. Choose the campaign you would like the Cforms form to add the lead to and copy the ‘value’ (whatever is in between the quotation marks <option value=”XXXXXXXXXXXXXXX”> ).

Then add another hidden field to your Cforms form. Call it ‘Campaign_ID|’ then without spaces your Salesforce campaign ID number (same process as above).

Make sure to update the form when you are done.

Step 3 – We’re almost done

Click to enlarge

There is one more step to take. Underneath your form in Cforms there are options. Open up “Core Form Admin / Email Options” and you’ll find an option: “Use custom input field NAMES & ID’s”. By default, Cforms uses generic field names and ID’s for the input fields. Salesforce does not understand these names. By checking this box it is ensuring that Salesforce will be able to understand the information you are sending it. Make sure to update the form again and you’re good to go.

Appendix – or “What if I don’t want ALL my forms to be submitted to Salesforce?”

As I mentioned above, once you make these adaptations you should be aware that EVERY form you create with Cforms on your site will try to submit the information to Salesforce. If you do not want this to happen there are a few more lines of code that need to be added to “my-functions.php”.

Before we make the changes you’ll need to know the ID of the forms you want submitted. If you go to the “Form Settings” page you will see at the top a form navigation system to navigate between forms.

On the right you’ll see numbers in white ovals and one in a blue circle. Each of those number correspond with a different form and that number is the form ID. Navigate to the form you want, and then see which number is in blue.

Next: Open up “my-funtions.php” and find the line

$form   = $cformsdata['data'];

After that line insert the following code:

if ( $formID == '3'){

Replace the number ‘3’ with the ID of the form you want sent to Salesforce. If there are several forms you want sent then use this code instead:

if ( $formID == '3' || $formID == '4' ){

Again, replace the numbers to the corresponding numbers of the forms you want send to Cforms. For each form you want included add another || $formID == ‘N’ before the closing bracket.

WAIT! You’re not done yet!!! If you leave it as-is you will break Cforms.

Find the end of the function. It will end with a curly bracket.

fclose($fp);

}

between the fclose($fp); and the } add another curly bracket ‘}’ like so:

fclose($fp);

}

}

If you skip this stage you will probably break Cforms. And NOW, don’t forget to save the changes you’ve made.

NOW you’re done!

Questions? Comments? Please feel free to ask below!


    36 thoughts on “How To Integrate Salesforce With Cforms (and have multiple Salesforce Web to Lead forms in WordPress)

    1. Great work on the curl connection. It works for me "without" AJAX enabled but presents these errors:

      Warning: fopen (https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in /home/content/73/6176573/html/wp-content/plugins/cforms/my-functions.php on line 193

      Warning: fpassthru(): supplied argument is not a valid stream resource in /home/content/73/6176573/html/wp-content/plugins/cforms/my-functions.php on line 194

      Warning: fclose(): supplied argument is not a valid stream resource in /home/content/73/6176573/html/wp-content/plugins/cforms/my-functions.php on line 195

      Any thoughts?

      Matt

      1. Matt – did you ever get this working? I have exactly the same problem. I confirmed the entry in PHP.ini is there (allow_url_fopen=On) and my web host says it's not them causing the problem. Thanks!

    2. Hi Matt,

      Thanks for the kind words.

      You may have to manually set the php.ini on your sever to allow for fopen() to work with HTTP://

      Check with your hosting company or set allow_url_fopen=On in your PHP.ini file.

      Your host also might not allow http as part of their firewall policy.

      Let me know if this helps.

    3. Works great and has saved me tearing the last of my hair out. Almost.

      It seems to pass on every field perfectly… that doesn't contain a URL. If I have a text field asking for their Website or a hidden field containing the URL of the page they're submitting the form (custom field in Salesforce) from, it doesn't get picked up by Salesforce (just left blank).

      If there's any work around for that, or just something dumb I'm not seeing, I'd appreciate the insight.

      Thanks again,

    4. Hey Rene,

      Hmmm, I haven't dealt with forms needing that yet.

      It may be a urlencode issue. Try changing line 22 in the code for my-funcitons.php from

      `$post_items[] = str_replace(' ','_', strtolower($key)) . '=' . $value;`

      to

      `$post_items[] = str_replace(' ','_', strtolower($key)) . '=' . urlencode($value);`

      Another issue it may be, salesforce is very particular about the input names. If they don't match exactly, it won't pick it up. My code takes the most common Labels and converts them to fit salesforce (lowercases, adding underscores for spaces) it's possible that the fields you are using need different formatted names. (like the campaign id field) if they do, add an elseif condition into line 19.

      Let me know if that helps.

    5. Hi

      As soon as I add this code to my functions php file, the submission process just hangs. I keep getting the "One moment please…" message. As soon as I comment out the line, the submission proceeds fine.

      Any idea what is happening? Thanks a lot for any help

    6. Hi,

      I'm having trouble with this modification. I double and triple checked the directions to ensure I have done exactly as stated, but the forms just hangs on submission–page load half way and hangs after clicked on submission. I do not use a campaign ID. Not sure if that is causing the issue.

      Also, there's another issue I'm experiencing… One of the forms doesn't accept Email at all. If the form is set up to require an email address, it will complain an invalid email address even if I had input a valid email address. On the error screen, it doesn't show the email I had just input. Instead, it shows "None".

      I'm using the latest version of CformsII 11.6.1 with latest wordpress 3.0.1.

    7. I got this to work really well, but had to use a lot of 'elseif' statements to rename my field labels to match Salesforce field names.
      Is there a way to make Google Adwords lead tracking work?

    8. Thanks so much- I'm integrating with a different CRM (vTiger) but this worked! YAY! If it wasn't for your help I'd still be going nuts trying to figure out what was wrong.

      It was that pesky AJAX error. I disabled ajax and (knock on wood) everything is working.

    9. So happy to find this. I'm working with a client now who heavily uses Salesforce and the simple WordPress plugin just isn't cutting it for their needs. I'm looking forward to trying this. I'm not a coder so it should be fun.

    10. Thanks for a great post… I've been looking for something like this!

      It works beautifully except that when user clicks submit the form hangs on "One moment please…" without going to the "success" message. The form goes into SF just fine though.

      Any thoughts on what's causing this issue?

    11. Hey, great tweak! I've been editing my forms using this strategy.

      Just one question: how do you capture a text area? I created a text field in SalesForce titled "comments or questions" and I'd like the form data to feed into that field. Any advice would be greatly appreciated.

      Thanks!

    12. Hi! This post was really helpful. But when the form gets submitted, the values are not getting captured in SFDC. They are all blank. I have set the oid correcly and all the fields have the id provided by SFDC. Please help!

    13. Hi jack,

      Thanks a lot for this nice tip. The problem I have is that CURL request is construct with fields label and not field id or name. Since my labels are in french, the request gives an empty lead in Sales Force. When I switch the labels to english, it works great.
      Any clue to catch field name or id in the loop instead of label with Cforms II API ?

    14. Hi Jack,

      Your solution for integrating cforms 2 with salesforce is a life saver. My form submission works fine except for one problem. I have some checkbox fields that don't seem to go through with submission. The fields have the correct id (matching the one generated by Salesforce) but for some reason the value is not getting submitted. Is there something special that needs to be done for it to work? I noticed that there is no way to set the value of the checkbox field (if submitted) in the cform settings.

      Your help is very much appreciated.

      Thanks,
      Reshma

    15. It's funny how things work. Just after I asked you this question, I found the answer on cforms forum. If I want to have a value associated with a checkbox field, I need to set it in the Field Label for the checkbox – field label | value

      That seem to work in transmitting the checkbox to Salesforce. Sorry to bother you!

      Thank you again for this solution. I would have had to consider making my own form if not for this solution.

    16. thanks for the helpful post, however i was wondering if you a easy way to populate select boxes with cforms from the mysql database? for instance i have a selectbox named subject and i want as options all subjects +150 to be selectable, for sake of simplicity the database has a table subjects with id and name and i want to populate it with all names … any suggestions? big thanks for any help you can give :)

    17. This works great with the premade fields in salesforce but not with custom made fields or picklists, anyone else have this problem?

      The formatting is different in the web-to-lead generator:

      works:

      Country

      doesnt work:

      Comments:

      thanks for posting this!

      -lino

      1. I found that the name has to fix exactly, so on the instance of the plugin that I used I had to hack up the code a bit to take out punctuation before sending it on to salesforce. I’d imagine that the colon after “Comments:” is what is causing it.

    18. Love this and works when ajax is off as mentioned.

      Can’t seem to get any Saleforce multi pick list to come through though as they all have coded ID (numbers and letters) for example 000N8002nHG, which never seem to work. I’ve tried all types of configurations.

      EG: Interest[id:000N8002nHG]#Select One

      etc. Any clues would be much appreciated, haven’t seen anyone mention SF multi pick lists.

      1. So glad you found it helpful!
        Hmm, I’ll see if I can figure something out. I haven’t worked with the client I did this for in a while, but I’ll let you know if I find something…

    19. @miklr I had the same problem with pick lists. The issue is they generate some weird crazy ID’s for the web-to-lead and they are CASE SENSITIVE. So this line:

      $post_items[] = str_replace(‘ ‘,’_’, strtolower($key)) . ‘=’ . $value;

      Actually causes the IDs to not match. However fixing that still wasn’t working so I moved the whole function to the “Custom user data input filter (non ajax) which probably isn’t the best spot, but it works. Also note if you use a checkbox group cforms returns that as an array so you have to set the value to one item in the array as Salesforce can’t process the array. I did this:

      if ( $key == ‘ARRAY RESULT’ ) {
      $temp_array = $value;
      $value = $temp_array[0];
      }

    20. Fantastic post! How do I handle a check box group (please check all that apply)? Since the Saleforce form only allows text, will all those values just end up as text or can the yes values (7 yes out of 14 choices) be mapped to custom fields in Salesforce? If so, how?

      1. It’s been a while since I played with this code. But the concept is having the name of the form element match with a field in the Salesforce form. To figure out what it should be specifically I’d recommend crafting the form you want in Salesforce, crack open their code, and see how they deal with it. You may not need too many modifications to get the form to work in Cforms II.

    21. Jack, you’re a true Cforms Master! I thought I was, but I’ve been trying to do this for a while and finally, thanks to you, it’s a reality. My business partners are truly impressed. I’m happy that over a year later this hack still works.

      Thank you!

      Svet

    22. Thanks for the code snippet and the instructions. They are beautiful and extremely useful. I have one issue, getting the custom fields from the form into salesforce is giving me a huge headache. Did anyone figure it out? I will keep running my head into a brick wall until I hear from you.

      Thanks In Advance

    23. Hey Matt (from the first post), did you ever get this to work? I have the exact same problem you had and I am ripping my hair out. The salesforce data is being captured without issue, but it is supposed to redirect after that. I have the same error message.

    24. Hi,

      old post but still valid. I have a very specific question, maybe you can help me.

      I use cforms in a multisite. It works although it is Not supported officially, but when it comes to the my-functions.php it is not working anymore. Do you know where in the cforms code or in which file the plugin executes/references the file?

      Use Case
      Site 1: form submitted -> my-functions.php is executed
      Site 2: form submitted -> my-functions.php is NOT executed

      No idea where to find the issue.
      Thx in advance
      Markus

    25. Hi Jack,

      thx for your quick response. Yes, I do use two different themes. Have you any idea how I can check, if the my-functions.php is included or not? I’m using the ajax method with cForms.

    Leave a Reply