You are here

function salesforce_contact in Salesforce Suite 5

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

handle contacts in salesforce

3 calls to salesforce_contact()
salesforce_lead in includes/salesforce_api.inc
handle leads in salesforce
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 66
integration of Drupal and Salesforce.com. Original author: Steve Mckenzie Current maintainer: Victor Kane

Code

function salesforce_contact($op, $account, $params = array()) {
  $salesforce = salesforce();
  $admin = user_access('administer salesforce');
  switch ($op) {
    case 'insert':
    case 'update':
      $contact = new sObject('Contact', 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 contact object
      foreach ($params as $param => $param_value) {
        $contact->{$param} = $param_value;
      }

      // query salesforce to check if the user already has a contact with the supplied email
      $contact_lookup = $salesforce
        ->query("SELECT id FROM contact WHERE email = '{$account->mail}'");

      // if we get back multiple results, we only bother looking at the first one.
      if (is_array($contact_lookup['records'])) {
        foreach ($contact_lookup['records'] as $row) {
          $contact_id = $row->id;
          break;
        }
      }
      else {
        $contact_id = $contact_lookup['records']->id;
      }

      // insert method
      if ($op == 'insert' && count($contact_lookup['records']) == 0) {
        $result = $salesforce
          ->create($contact);
        if ($result['success'] == 'true') {
          _salesforce_insert('contact_id', $result['id'], $account);
          if (user_access('administer salesforce')) {
            drupal_set_message(t('a contact 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 = 'contact_insert';
        }
      }
      else {
        if (!$account->salesforce['contact_id']) {
          $account->salesforce['contact_id'] = $contact_id;
          _salesforce_insert('contact_id', $account->salesforce['contact_id'], $account);
        }
        $contact->id = $account->salesforce['contact_id'];

        // why am i unsetting this you may ask? i'm not sure but salesforce tells me too... wtf?
        unset($contact->values['Company']);
        $result = $salesforce
          ->update($contact);
        if ($result['success'] == 'true') {
          if ($admin) {
            drupal_set_message(t('updated contact 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']) {
            default:
              $error = 'contact_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 contact 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;
  }
}