function salesforce_lead in Salesforce Suite 5
Same name and namespace in other branches
- 5.2 includes/salesforce_api.inc \salesforce_lead()
handle leads in salesforce
3 calls to salesforce_lead()
- salesforce_contact_form in ./
salesforce.module - handle the contact form in a special way since we're building the $user object manually from the contact form values
- salesforce_user in ./
salesforce.module - Implementation of hook_user().
- _salesforce_cron_run in ./
salesforce.module - run a logged cron item
File
- includes/
salesforce_api.inc, line 155 - integration of Drupal and Salesforce.com. Original author: Steve Mckenzie Current maintainer: Victor Kane
Code
function salesforce_lead($op, $account, $params = array()) {
// grab a salesforce api connection
$salesforce = salesforce();
// avoid fatal error on salesforce query with null element in query
// catch sf authentication problem (drupal message already set)
if (!$salesforce) {
return;
}
$admin = user_access('administer salesforce');
switch ($op) {
case 'insert':
case 'update':
default:
// create the main salesforce object
$lead = new sObject('Lead', null, array(
'Company' => $account->company,
'FirstName' => $account->first_name,
'LastName' => $account->last_name,
'Phone' => $account->phone,
'Fax' => $account->fax,
'Email' => $account->mail,
));
// add any additional params to the lead object
foreach ($params as $param => $param_value) {
$lead->values[$param] = $param_value;
}
// query salesforce to check if the user already has an account with the supplied email
$lead_lookup = $salesforce
->query("SELECT id FROM lead WHERE email = '{$account->mail}'");
// if we get back multiple results, we only bother looking at the first one.
if (is_array($lead_lookup['records'])) {
foreach ($lead_lookup['records'] as $row) {
$lead_id = $row->id;
break;
}
}
else {
$lead_id = $lead_lookup['records']->id;
}
// insert method
if ($op == 'insert' && count($lead_lookup['records']) == 0) {
$result = $salesforce
->create($lead);
if ($result['success'] == 'true') {
_salesforce_insert('lead_id', $result['id'], $account);
if (user_access('administer salesforce')) {
drupal_set_message(t('a lead was created in salesforce for the user <strong>@user</strong>', array(
'@user' => "{$account->first_name} {$account->last_name}",
)));
return array(
'id' => $result['id'],
'status' => 'inserted',
);
}
}
else {
$error = 'lead_insert';
}
}
else {
if (!$account->salesforce['lead_id']) {
$account->salesforce['lead_id'] = $lead_id;
_salesforce_insert('lead_id', $account->salesforce['lead_id'], $account);
}
$lead->id = $account->salesforce['lead_id'];
$result = $salesforce
->update($lead);
if ($result['success'] == 'true') {
if ($admin) {
drupal_set_message(t('updated lead in salesforce for the user <strong>@user</strong>', array(
'@user' => "{$account->first_name} {$account->last_name}",
)));
return array(
'status' => 'updated',
);
}
}
else {
switch ($result['errors']['statusCode']) {
case 'CANNOT_UPDATE_CONVERTED_LEAD':
// do an update contact object but first find it..
return salesforce_contact('update', $account, $params);
case 'INVALID_ID_FIELD':
if ($admin) {
db_query("DELETE FROM {salesforce_users} WHERE lead_id = '%s'", $account->salesforce['lead_id']);
drupal_set_message(t('the lead for the user <strong>@user</strong> was removed from salesforce', array(
'@user' => "{$account->first_name} {$account->last_name}",
)));
return array(
'status' => 'lead_removed_from_salesforce',
);
}
break;
default:
$error = 'lead_update';
break;
}
}
}
// handle error control and log errors
if ($error) {
if ($admin) {
drupal_set_message(t('an error occured <strong>[%error]</strong> during a salesforce lead task for the user %user with the messasge - "%message"', array(
'%user' => theme('username', $account),
'%message' => $result['errors']['message'],
'%error' => $error,
)), 'error');
}
_salesforce_log_user($error, $result['errors']['message'], $account);
return array(
'error' => $error,
);
}
break;
}
}