Posted by btlife on March 12, 2011 at 11:17pm
3 followers
Jump to:
| Project: | Salesforce Webform Data Integration |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs work |
Issue Summary
When creating the Form Components it would be nice to know what fields are Required for the SalesForce object that the form is based on.
This can go with the previous feature request of only showing relavent fields, or it can be modified to grab all required fields regardless of SalesForce Object Type (i.e. how it works without the Feature Request to filter out fields that don't belong to the SalesForce Object)
salesforcewebform_fields.inc (new function salesforcewebform_required_fields)
<?php
// required fields in SalesForce
function salesforcewebform_required_fields($formSFObjectTypeCode) {
//Define Implementation for hook_salesforcewebform_fields
$fields = module_invoke_all('salesforcewebform_fields', $formSFObjectTypeCode);
return $fields;
}
?>salesforcefields.module (new function salesforcefields_salesforcewebform_required_fields)
<?php
/*
** This function returns an array of the sales force field objects that are required for the object type
**
** Required is defined as NOT nillable, createable, and NOT defaultedOnCreate
**
** Assumption: There are no fields that are required and ONLY updateble (i.e. updateable but not createable)
**
** NOTE: The reason for sending the whole field object back, instead of just the name/value key pair, as the
** function above does(salesforcefields_salesforcewebform_fields) is to allow a future update to automatically
** create the webform field components. Sending the whole filed object back will allow a check on field->type to
** allow the form component type to be determined for auto-creating webform field components
*/
function salesforcefields_salesforcewebform_required_fields($form_sf_object_typecode)
{
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;
}
$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->nillable && !$field->createable & !$field->defaultedOnCreate) {
$fields[$key] = $field->$field;
}
}
}
}
}
return $fields;
}
?>salesforcewebform.module (around line 77 basically adding a new section to the main if statement)
<?php
...
// integrate the SalesForce form into the form
$pos = array_search('mailsettings', array_keys($form)) + 1;
$form = array_merge(array_slice($form, 0, $pos), $salesforce_form, array_slice($form, $pos));
}
//BEGIN NEW CODE
elseif ( $form_id == 'webform_components_form') {
$node = $form]'#parameters'][2];
if($node->use_salesforce == 1) {
module_load_include('inc','salesforcewebform','salesforcewebform_fields');
$object_typecode = ($node->salesforce_object) ? $node->salesforce_object : 'l';
foreach(salesforcewebform_required_fields($object_typecode) as $key=>field) {
$missing = TRUE;
foreach($node->webform['components'] as $component) {
if($component['form_key'] == strtolower($field_name)) {
$missing = FALSE;
}
}
if($missing == TRUE) {
drupal_set_message(t('Missing required field: ' . $field->label), 'error');
}
}
}
} //
//END NEW CODE
// 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
...
?>
Comments
#1
The above change will show RED druple message at the top of the form if the Required Fields for the selected SalesForce Object type are not defined in the form components list.
It checks the SalesForce API Key name (field->name) against the Drupal webform form_key (component->form_key).
On that thought, just noticed a type in the above code
reposting it with the typo fixed:
salesforcewebform.module(around line 77 basically adding a new section to the main if statement)
<?php
...
// integrate the SalesForce form into the form
$pos = array_search('mailsettings', array_keys($form)) + 1;
$form = array_merge(array_slice($form, 0, $pos), $salesforce_form, array_slice($form, $pos));
}
//BEGIN NEW CODE
elseif ( $form_id == 'webform_components_form') {
$node = $form]'#parameters'][2];
if($node->use_salesforce == 1) {
module_load_include('inc','salesforcewebform','salesforcewebform_fields');
$object_typecode = ($node->salesforce_object) ? $node->salesforce_object : 'l';
foreach(salesforcewebform_required_fields($object_typecode) as $key=>field) {
$missing = TRUE;
foreach($node->webform['components'] as $component) {
if($component['form_key'] == strtolower($field->name)) {
$missing = FALSE;
}
}
if($missing == TRUE) {
drupal_set_message(t('Missing required field: ' . $field->label), 'error');
}
}
}
} //
//END NEW CODE
// 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
...
?>
#2
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
<?php
function salesforcefields_salesforcewebform_required_fields($form_sfobject_typecode)
{
//drupal_set_message(t('Form Object Type: ' . $form_sfobject_typecode));
// Filter the return array by the formSFObjectType
// Have to break apart the multiples
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($objtype->name == 'Lead' && $field->name == 'Company') {
$field->label = $objtype->name . ':' . $field->label;
$fields[$key] = $field;
} elseif ($field->name && $field->label && !$field->nillable && $field->createable && !$field->defaultedOnCreate) {
$field->label = $objtype->name . ':' . $field->label;
$fields[$key] = $field;
}
}
}
}
}
return $fields;
}
?>
#3
GOOD STUFF! On my issue queue.
#4
I cant help out in 2.x branch see: http://drupal.org/node/1092344
#5
I like the idea of the red text - I will add this next as an add-on to #1089664: Filter the SalesForceField Key Drop-Down with only the relevant fields for the SalesForce object selected for the form.