Currently the drop-down contains all available fields from Lead, Contact, and Account.

The following will filter the list to only the fields for the selected SalesForce Object Type for the webform being created:
form salesforce object type code is defined as 'l'-Lead, 'c'-Contact,'a'-Account,'p'-'Person Account','m'-'Account + Contact'
salesforcewebform_fields.inc -- pass object type code into salesforcewebform_available_fields

// available fields in SalesForce
// NEW CODE -- Add parameter $form_sfobject_typecode
function salesforcewebform_available_fields($form_sfobject_typecode) {
   $fields = array(    
    //Special Field to block Processing
    0 => '--Do Not Map to SalesForce--',
    );

  //Define Implementation for hook_salesforcewebform_fields
// NEW CODE -- pass parameter to salesforcewebform_fields
  $fields = array_merge($fields, module_invoke_all('salesforcewebform_fields', $form_sfobject_typecode));

  return $fields;
}

salesforcefields.module -- only return fields that belong to the object type code

// NEW CODE -- Accept new parameter $form_sf_object_typecode
function salesforcefields_salesforcewebform_fields($form_sf_object_typecode)
{
// BEGIN NEW CODE -- Translate sf object type code, to SF object names
  switch($form_sf_object_typecode) {
    case "l":
      $types = array('Lead');
    break;
    case "c":
      $types = array('Contact');
    break;
    case "p":
      $types = array('Account');
    break;
    case "a":
      $types = array('Account');
    break;
    case "m":
      $types = array('Contact','Account');
    break;
  }
  // END NEW CODE
   $real_fields = salesforce_api_describeSObjects(array('Lead', 'Contact', 'Account'));
  if ($real_fields) {
    foreach($real_fields as $objtype) {
// NEW CODE -- Add a check to see if the current SF object is one of the ones this form uses
// NEW CODE -- If it is then add the fields to the list
	  if(in_array($objtype=>name, $types)) {
		  foreach ($objtype->fields AS $key => $field) {
			if ($field->name && $field->label) {
			  $fields[$field->name] = $field->label;
			}
		  }
	  } // NEW CODE END
    } 
  }
  return $fields;
}

salesforcewebform.module -- add object type code to all salesforcewebform_available_fields calls (there are 2, one when editing webform components and one when processing the form submit)

...
/**
 * Allow for editing webforms with SalesForce connection information and modifying the
 * each webform to post to SalesForce.
 */
function salesforcewebform_form_alter(&$form, &$form_state, $form_id) {
...
	// handle editing fields on the webform
	elseif ( $form_id == 'webform_component_edit_form' ) {
 		// if the user has specified the SalesForce action, then we will be posting the form to SalesForce
		if ( $form['#parameters'][2]->use_salesforce == 1 ) {
	    module_load_include('inc', 'salesforcewebform', 'salesforcewebform_fields');
// NEW CODE -- get the object type code 
$object_typecode = ($node->salesforce_object) ? $node->salesforce_object : "l";
// NEW CODE -- add it to the salesforcewebform_available_fields call
		  foreach(salesforcewebform_available_fields($object_typecode) as $sfwafkey=>$sfwafvalue) {
        if (!is_array($sfwafvalue)) {
          $sfwafkey = strtolower($sfwafkey);
          $sfwaf_array[$sfwafkey] = $sfwafvalue;
        }
...
}
...
function salesforcewebform_process($node, $form, $form_state) {
  $object_typecode = ($node->salesforce_object) ? $node->salesforce_object : "l";
...
	$all_fields = array();
// NEW CODE -- Add object typecode to the salesforcewebform_available_fields call        
	$master_fields = array_merge(salesforcewebform_available_fields($object_typecode), salesforcewebform_load_custom_fields(NULL));
	$submission = array();
...
}
...

I apologize if I am not submitting purposed changes correctly, but I am trying to post them here in case someone can use them, while I am making them to my copy. I also apologize for any typos, I am working off a remote box.

Thanks

Comments

btlife’s picture

Modified it so it will filter out fields that can't be sent in a create or update.

// NEW CODE -- Accept new parameter $form_sf_object_typecode
function salesforcefields_salesforcewebform_fields($form_sf_object_typecode)
{
// BEGIN NEW CODE -- Translate sf object type code, to SF object names
  switch($form_sf_object_typecode) {
    case "l":
      $types = array('Lead');
    break;
    case "c":
      $types = array('Contact');
    break;
    case "p":
      $types = array('Account');
    break;
    case "a":
      $types = array('Account');
    break;
    case "m":
      $types = array('Contact','Account');
    break;
  }
  // END NEW CODE
   $real_fields = salesforce_api_describeSObjects(array('Lead', 'Contact', 'Account'));
  if ($real_fields) {
    foreach($real_fields as $objtype) {
// NEW CODE -- Add a check to see if the current SF object is one of the ones this form uses
// NEW CODE -- If it is then add the fields to the list
      if(in_array($objtype=>name, $types)) {
          foreach ($objtype->fields AS $key => $field) {

// NEW CODE -- Added createable and updateable check to filter out fields that can't be updated, or created through the API
            if ($field->name && $field->label && ($field->createable || $field->updateable)) {
              $fields[$field->name] = $field->label;
            }
          }
      } // NEW CODE END
    }
  }
  return $fields;
}
btlife’s picture

Modified it again, to add the SalesForce object type and whether it is a required field or not to the label that appears in the drop-down. There is a Special Case, as noted in the SalesForce API, that a Lead can be a "Person Lead" which means they don't have a company. Company field is still required to be sent to SalesForce but it can be nil if you want to create the special "Person Lead".
From the SalesForce API Documentation for the Lead Object:
"If person account record types have been enabled, and if the value of the Company field is null, the lead converts to a person account"

So if your SalesForce account doesn't support person account types, or you don't want to use them, the Company field is required and must not be empty for Leads.

The Special Case is needed because Company field is defined as nillable, createable, and not defaultedOnCreate, unlike the other required fields which are NOT nillable, createable and NOT defaultedOnCreate.

salesforcefields.module

function salesforcefields_salesforcewebform_fields($form_sfobject_typecode)
{
  
  switch($form_sfobject_typecode) {
    case "l":
      $types = array('Lead');
    break;
    case "c":
      $types = array('Contact');
    break;
    case "p":
      $types = array('Account');
    break;
    case "a":
      $types = array('Account');
    break;
    case "m":
      $types = array('Contact','Account');
    break;
  }

  $real_fields = salesforce_api_describeSObjects(array('Lead', 'Contact', 'Account'));
  if ($real_fields) {
    foreach($real_fields as $objtype) {
      if (in_array($objtype->name, $types)) {
        foreach ($objtype->fields AS $key => $field) {
          if ($field->name && $field->label && ($field->createable || $field->updateable)) {
            // Special Case if a Lead Object Type and person accounts are supported and desired (i.e. Doesn't have a company)
            // Company is still required but can be nil 
            if($objtype->name == 'Lead' && $field->name == 'Company') {
              $fields[$field->name] = $objtype->name . ':' . $field->label . '(required special)';
            } elseif(!$field->nillable && $field->createable && !$field->defaultedOnCreate) {
              $fields[$field->name] = $objtype->name . ':' . $field->label . '(required)';
            } else {
              $fields[$field->name] = $objtype->name . ':' . $field->label;
            }
          }
        }
      }
    } 
  }
  return $fields;
}

chriscalip’s picture

Assigned: Unassigned » chriscalip
chriscalip’s picture

Assigned: chriscalip » Unassigned

I cant help out in 2.x branch see: http://drupal.org/node/1092344

obsidiandesign’s picture

Status: Active » Fixed

Code in #1-#3 has been added to the 6.x-2.x branch. This idea is AWESOME and provides a much cleaner, usable, less confusing setup of the Field Key dropdown. Thanks btlife for providing all of this!

Bryan O'Shea

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Mtoag’s picture

Thanks btlife