You are here

function salesforce_lead in Salesforce Suite 5.2

Same name and namespace in other branches
  1. 5 includes/salesforce_api.inc \salesforce_lead()

handle leads in salesforce

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();
  $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 %user', array(
              '%user' => "<strong>{$account->first_name} {$account->last_name}</strong>",
            )));
            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 %user', array(
              '%user' => "<strong>{$account->first_name} {$account->last_name}</strong>",
            )));
            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 %user was removed from salesforce', array(
                  '%user' => "<strong>{$account->first_name} {$account->last_name}</strong>",
                )));
                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;
  }
}