Categories
General

Brilliant Web-To-Lead for Salesforce with file uploads

If you are using WordPress, you can send file attachments to Salesforce from your website easily with Brilliant Web To Lead plugin. Technically we are not storing the files in Salesforce, in stead we are storing the URL to the file to the Salesforce, where you can then pick up the file and process it as you wish.

Install the plugin Brilliant Web-To-Lead for Salesforce and we’ll move to uploads. You can even copy and paste the “script” you got from Salesforce in plugins’ admin section. It will have the fields already and it will save you from typing.

You need to add the field for the attachment and name it something, E.g. “attachment”. Choose type to be “HTML”. Then paste this to your options part:

<input type="file" name="fileToUpload" id="fileToUpload" class="fileupload"><label for="fileToUpload" id="fileuploadlabel"><input type="text" id="fileToUploadText" name="fileToUploadText" value="Upload attachment"></label>

This will print out the file upload button to your website, where a user can choose the file locally from his/her computer.

Remember that you need to create the field in Salesforce first in order to get the link to the attachment. You do this in Salesforce by finding an API Name for your field (e.g. Custom_Field_Example__c). If it doesn’t end in ”__c” it’s not the API name and will not work! Check my previous post about Salesforce file upload hack.

Hacking the Brilliant Web-to-Lead for Salesforce

Unfortunately this plugin does not have multipart enctype definition. We have to fork the script a little. Here’s how we do it:

  1. First, backup your WordPress installation, especially plugins folder and themes folder.
  2. Then activate your debug log by adding the following to the wp-config.php file.
define( 'WP_DEBUG_LOG', true );
  1. Open your favourite FTP/SSH program and head to file wp-content/plugins/salesforce-wordpress-to-lead/salesforce.php.
  2. Find the following line in the file and make the changes:

Before:

$content .= "\n".'<form id="sf_form_'.$sf_form_id.'" class="'.($options['wpcf7css'] ? 'wpcf7-form' : 'w2llead'.$sidebar ).' '.$label_location.'" method="post" action="'.$action.'">'."\n";

After:

$content .= "\n".'<form id="sf_form_'.$sf_form_id.'" class="'.($options['wpcf7css'] ? 'wpcf7-form' : 'w2llead'.$sidebar ).' '.$label_location.'" method="post" action="'.$action.'" enctype="multipart/form-data">'."\n";

This will let you actually get file attachments via web form.

  1. Head over to your theme’s functions file at: /var/www/yousite.com/wp-content/themes/YOUR_THEME/functions.php
  2. Add the following script at the end of functions.php file. Remember to replace the /var/www/susannelumme.com/wp-content/uploads/2020/09/ with your folder structure.
add_filter( 'salesforce_w2l_post_data', 'salesforce_w2l_post_data_example', 10, 3 );

function salesforce_w2l_post_data_example( $post, $form_id, $form_type ){
    $post['test'] = 'test';

    $target_dir = "/var/www/susannelumme.com/wp-content/uploads/2020/09/";

    $target = $target_dir . basename($_FILES["fileToUpload"]["name"]);

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target)) {
                $post['test'] =  "File uploaded ". basename( $_FILES["fileToUpload"]["name"]). " to server.";
        } else {
                $error = $_FILES['fileToUpload']['error'];
                $post['test'] = "Ooops, we have an error: " . $error;
        }

$post['mycustomfieldinsalesforce__c'] = basename($_FILES["fileToUpload"]["name"]); 
error_log( 'POST ARGS = '.print_r( $post, 1 ) );
return $post;
}

That’s it! You are now officially collecting file attachments and storing them to your web server.

  1. At this point, try your form. You should already know how to add the form to any WordPress page with short-code [salesforce form=”X”]. Do it!
  2. Then navigate to your website and find the form. Fill it in with attachment. Check, did you got any errors or “Success” message?
  3. Check your debug log by opening a file at /wp-content/debug.log.
  4. Edit your wp-config.php and turn off the debug log.
define( 'WP_DEBUG_LOG', false );

However, I strongly recommend that you will create a new folder under WordPress’ uploads and hash the file names (PHP’s md5 function), so that the files are not in clear format (E.g. http://www.yoursite.com/wp-content/uploads/file_from_your_customer.pdf) for everyone on the Internet.

You can also add some extra safety by making a cron script, which will delete the files under this specific folder after a few days. That should also give the desired boost for your sales team :). They will have to contact the customer before the file expires.

I also recommend checking what type of files you allow visitors to upload by adding if (in_array($yourfile, $acceptedfiles_array)) { and then do the move_uploaded_file. For safety reasons I would only recommend accepting PDF’s and image files.

Possible errors

I got a permission denied error

You might need to adjust your WordPress folder rights. You need to give decent permissions to your WordPress user (pretty often either apache or www-data) so that your web server is able to upload the attachment file to your server.

The file is not uploading

Check the folder you specified for $target_dir. Is that correct? Do you have a folder like this? Can you access it via URL? Also check that your file attachments are not too big. To check the maximum size for uploads, you can check it from WP media section in admin. Go to Media > Add New. Below the Select Files button you can see the maximum size of a file your WordPress/web server is able to handle, E.g. 2MB. Change this setting in php.ini.

Something else is wrong

Check the debug log. You can also add another function to your functions.php file to actually receive error messages, especially, if you are not admin in WordPress. Check this function salesforce_w2l_cc_admin_email_list at the plugins page.

Other errors?

Please let me know.

Let me know, how it went. Cheers!

3 replies on “Brilliant Web-To-Lead for Salesforce with file uploads”

Hi ! Thank you for the great posts on this. On salesforce, in my “file link” field, im only getting the file name instead of the link. How do I fix this issue?

Hi Marc! So you have file.pdf instead of https://www.yourwordpresssite.com/uploads/2022/01/file.pdf in Salesforce? Change $post[‘mycustomfieldinsalesforce__c’] = basename($_FILES[“fileToUpload”][“name”]); to $post[‘mycustomfieldinsalesforce__c’] = “https://yousite.com/yoursitefolderstucture/” . basename($_FILES[“fileToUpload”][“name”]);. However, your file should be on your server and you should be able to access it via URL. If you want to have a clickable link in Salesforce, you need to pass the URL as well. In my case, the file would be at https://susannelumme.com/wp-content/uploads/2020/09/file.pdf, but in Salesforce it will show just file.pdf.

Howdy Susanne, you have given an opportunity on “Brilliant Web-To-Lead for Salesforce with file uploads”. Your explanation is about Brilliant Web-To-Lead for Salesforce and also given reference which was given step by step. It was creative and new. Thanks for sharing your info!!!

Leave a Reply

Your email address will not be published. Required fields are marked *