How to: Fix the Gravity Forms Salesforce Plugin – Picklist (Multi-Select) problem

[Go Back]

salesforceThis is one of those problems that you scour the internet for hours and find no discernible solution to the problem: When using a Multi-select (which is a custom field in Salesforce) in the Web-to-lead Plugin  for Gravity Forms it will continuously send an Array() to the API. This is not what we want. We want it to send all of the values that the user selected.

It turns out that the problem is not with the API but with an unpatched version of the Web-to-lead plugin. The patch requires actually modifying the plugin, unfortunately. It has not been committed yet to an updated version so it’s something you’ll have to keep an eye out for. Nonetheless, it does fix it.

I implemented a fix I found at a developers site. There he has the initial code for his fix. This works but it doesn’t address a secondary problem: Salesforce limits Multi-select inputs to 40 characters. So if you combine a bunch of selections the user has made, it will cut them off since his script passes them as a single comma-separated string. I made a simple modification to his code that adds in semicolons (which tells Salesforce to separate the values) which fixes that issue with the input.

Here is the original code we will be modifying (lines 513 to 551 in version 2.2.4.4). You will find this in your /wp-content/plugins/gravity-forms-salesforce/salesforce.php file.

 

            if( is_array($field["inputs"]) ){
                $valuearray = array();
               //handling multi-input fields such as name and address
               foreach($field["inputs"] as $inputKey => $input){
                   $value = trim(rtrim(stripslashes(@$_POST["input_" . str_replace('.', '_', $input["id"])])));
                   $label = self::getLabel($input["label"], $field, $input);
                   if(!$label) { $label = self::getLabel($field['label'], $field, $input); }
                   if ($label == 'BothNames' && !empty($value)) {
                        $names = explode(" ", $value);
                        $names[0] = trim(rtrim($names[0]));
                        $names[1] = trim(rtrim($names[1]));
                        if(!empty($names[0])) {
                            $data['first_name'] = $names[0];
                        }
                        if(!empty($names[1])) {
                            $data['last_name'] = $names[1];
                        }
                   } else if ($label == 'description') {
                        $message = 'true';
                       $data['description'] .= "\n".$value."\n";
                   } else if($label == 'street') {
                        $data['street'] = isset($data['street']) ? $data['street'].$value."\n" : $value."\n";
                   } else if (trim(strtolower($label)) == 'salesforce' ) {
                        $salesforce = $value;
                   } else {
                        if(!empty($field['inputName']) && (apply_filters('gf_salesforce_use_inputname', true) === true)) {
                            $valuearray["{$field['inputName']}"][] = $value;
                        } elseif(!empty($field['adminLabel']) && (apply_filters('gf_salesforce_use_adminlabel', true) === true)) {
                            $valuearray["{$field['adminLabel']}"][] = $value;
                        } elseif((!empty($data["{$label}"]) && !empty($value) && $value !== '0') || empty($data["{$label}"]) && array_key_exists("{$label}", $defaults)) {
                            $data[$label] = $value ;
                        }
                   }
               }
               if(isset($valuearray["{$field['adminLabel']}"])) {
                    $data[$label] = implode(apply_filters('gf_salesforce_implode_glue', ', '), $valuearray["{$field['adminLabel']}"]);
               } elseif(isset($valuearray["{$field['inputName']}"])) {
                    $data[$label] = implode(apply_filters('gf_salesforce_implode_glue', ', '), $valuearray["{$field['inputName']}"]);
               }

 

This code does it’s job properly for everything BUT the multi-select. Before we begin be sure to backup the original file in case you need to revert. That part is very important.

I also take no responsibility for any modifications you make. With that said, open your file and replace those lines with this:

 

            if( is_array($field["inputs"]) || is_array($field["choices"]) ){
                $valuearray = array();
                $fieldtemp = array();
                $multi_input = true;

                // set multi-input array to loop through
                if ( is_array($field["inputs"])) {
                    $fieldtemp = $field["inputs"];

				}
				else {
					$fieldtemp = $_POST["input_" . $field["id"]];
					$multi_input = false;
					$label = self::getLabel($field["label"], $field);    
				}

			   //handling multi-input fields such as name and address or choices

			   foreach($fieldtemp as $inputKey => $input){
                   //set the value and label 
                   if ($multi_input == true) {
                       // inputs is an array
                       $value = trim(rtrim(stripslashes(@$_POST["input_" . str_replace('.', '_', $input["id"])])));
                       $label = self::getLabel($input["label"], $field, $input);
                   }
                   else {
                       // choices is an array
                       $value = $input;
                   }
				   if(!$label) { $label = self::getLabel($field['label'], $field, $input); }
				   if ($label == 'BothNames' && !empty($value)) {
					    $names = explode(" ", $value);
					    $names[0] = trim(rtrim($names[0]));
					    $names[1] = trim(rtrim($names[1]));
					    if(!empty($names[0])) {
					   		$data['first_name'] = $names[0];
					   	}
					   	if(!empty($names[1])) {
					   		$data['last_name'] = $names[1];
					   	}
				   } else if ($label == 'description') {
			   	   		$message = 'true';
					   $data['description'] .= "\n".$value."\n";
			   	   } else if($label == 'street') {
			   	   		$data['street'] = isset($data['street']) ? $data['street'].$value."\n" : $value."\n";
			   	   } else if (trim(strtolower($label)) == 'salesforce' ) {
			   	   		$salesforce = $value;
			   	   } else {
			   	   		if(!empty($field['inputName']) && (apply_filters('gf_salesforce_use_inputname', true) === true)) {
							$valuearray["{$field['inputName']}"][] = $input;
						} elseif(!empty($field['adminLabel']) && (apply_filters('gf_salesforce_use_adminlabel', true) === true)) {
							$valuearray["{$field['adminLabel']}"][] = $value;
						} elseif((!empty($data["{$label}"]) && !empty($value) && $value !== '0') || empty($data["{$label}"]) && array_key_exists("{$label}", $defaults)) {
					   		$data[$label] = $value ;
						}
				   }
			   }

               // after looping through multi-input fields set the value
			   if(isset($valuearray["{$field['adminLabel']}"])) {
			   		$data[$label] = implode(apply_filters('gf_salesforce_implode_glue', ';'), $valuearray["{$field['adminLabel']}"]);
			   } elseif(isset($valuearray["{$field['inputName']}"])) {
			   		$data[$label] = implode(apply_filters('gf_salesforce_implode_glue', ', '), $valuearray["{$field['inputName']}"]);
			   }

 

Once you upload your file you’ll notice that it should be working. If not, you need to make sure you have already following the initial instructions and added the corrent API Name field to your Admin Label and Parameter name. Here’s a photo of where it goes:

 

paramname

 

Hope this helps save some of you a headache.

 

UPDATE: This issue has since been fixed by the plugin author. I had emailed him some time ago with this fix and the changes have been incorporated into the plugin.

0 comments
Published May 31, 2013   –   Categories: How To, Plugins, Salesforce, Web-to-lead, Wordpress