You are here

function salesforce_event in Salesforce Suite 5

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

handle events (history) in salesforce

2 calls to salesforce_event()
salesforce_form_event_submit in ./salesforce.module
_salesforce_cron_run in ./salesforce.module
run a logged cron item

File

includes/salesforce_api.inc, line 263
integration of Drupal and Salesforce.com. Original author: Steve Mckenzie Current maintainer: Victor Kane

Code

function salesforce_event($op, $subject, $message, $account = NULL) {
  $account = _salesforce_select_account($account);
  $salesforce = salesforce();
  if (!$salesforce) {
    return array(
      'error' => 'NO_SALESFORCE_CONNECTION',
    );
  }
  $admin = user_access('administer salesforce');

  // contacts before leads
  if ($account->salesforce['contact_id']) {
    $id = $account->salesforce['contact_id'];
  }
  else {
    if ($account->salesforce['lead_id']) {
      $id = $account->salesforce['lead_id'];
    }
    else {
      return array(
        'error' => 'NO_CONTACT_OR_LEAD_ID',
      );
    }
  }

  // currently only setting up insert
  switch ($op) {
    case 'insert':
      $activityDate = new soapval('ActivityDateTime', 'dateTime', (string) $salesforce
        ->getServerTimestamp());
      $event = new sObject('Event', NULL, array(
        'WhoId' => $id,
        $activityDate,
        'Subject' => t('%subject - %date', array(
          '%subject' => $subject,
          '%date' => format_date(time(), 'large'),
        )),
        'Description' => $message,
        'DurationInMinutes' => 1,
      ));
      break;
  }
  $result = $salesforce
    ->create($event);
  if ($result['success'] == 'true') {
    if ($admin) {
      drupal_set_message(t('an event 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 {
    switch ($result['errors']['statusCode']) {

      /*
      case 'CANNOT_UPDATE_CONVERTED_LEAD':
      $contact_lookup = $salesforce->query("select id from contact where email = '$account->mail'");
      if (count($contact_lookup['records']) > 0) {
      $account->salesforce['contact_id'] = $contact_lookup['records']->id;
      } else {
      $error = 'event_insert';
      }
      break;
      */
      default:
        $error = 'event_insert';
        break;
    }
  }

  // handle error control and log errors
  if ($error) {
    if ($admin) {
      drupal_set_message(t('an error occured <strong>[%error]</strong> during a salesforce event 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, 1, serialize($event));
    return array(
      'error' => $error,
    );
  }
}