You are here

function salesforce_api_connect in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 5.2 salesforce_api/salesforce_api.module \salesforce_api_connect()
  2. 7 salesforce_api/salesforce_api.module \salesforce_api_connect()
  3. 7.2 salesforce_api/salesforce_api.module \salesforce_api_connect()

Creates an object used for communicating with the Salesforce server and performs a login to verify the API credentials.


$username: Username for Salesforce. An email address, most likely. If none passed, sitewide creds will be used

$password: Password to Salesforce account.

$token: Security token from Salesforce.

$reconnect: By default, subsequent calls to this function will return the same, already connected Salesforce object as preceding calls. Setting this variable to TRUE will cause a new connection to be established instead.

Return value

The Salesforce Client object used to communicate with the Salesforce server if successful or FALSE if a connection could not be established.

26 calls to salesforce_api_connect()
drush_salesforce_api_soql in salesforce_api/
Execute a SOQL query.
drush_salesforce_api_soql_describe in salesforce_api/
Examine a salesforce table. Allows for deep inspection using dot syntax similar to sql databases.
drush_salesforce_api_soql_show_tables in salesforce_api/
Duplicates the functionality of mysql's SHOW TABLES
drush_sf_import in sf_import/
Import data from Salesforce
drush_sf_import_sf_fieldmap in sf_import/
Info about a specific fieldmap import

... See full list


salesforce_api/salesforce_api.module, line 297
Defines an API that enables modules to interact with the Salesforce server.


function salesforce_api_connect($username = FALSE, $password = FALSE, $token = FALSE, $reconnect = FALSE) {
  static $sf = FALSE;

  // Return the previously connected object.
  if ($sf && !$reconnect) {
    return $sf;

  // Load up the sitewide API credentials if none were provided.
  $encrypted = variable_get('salesforce_api_encrypt', FALSE);
  $default_username = $encrypted ? salesforce_api_decrypt(variable_get('salesforce_api_username', '')) : variable_get('salesforce_api_username', '');
  $username = $username ? $username : $default_username;
  $password = $password ? $password : ($encrypted ? salesforce_api_decrypt(variable_get('salesforce_api_password', '')) : variable_get('salesforce_api_password', ''));
  $token = $token ? $token : ($encrypted ? salesforce_api_decrypt(variable_get('salesforce_api_token', '')) : variable_get('salesforce_api_token', ''));

  // Boolean, whether we are connecting with the default website user or not.
  $default_site_user = $username == $default_username;
  if (!salesforce_api_toolkit_installed()) {
    return FALSE;

  // Fail early if we didn't receive an API username, password, or token.
  if (empty($username) || empty($password) || empty($token)) {
    salesforce_api_log(SALESFORCE_LOG_SOME, 'Connection to Salesforce
      failed because API credentials have not been set.', array(), WATCHDOG_ERROR);
    return FALSE;

  // Attempt a login.
  $sf = salesforce_api_login($username, $password, $token);
  if ($sf) {

    // Mimick expired password state to debug.
    // $sf->login->passwordExpired = TRUE;
    if ($sf->login->passwordExpired) {
      if ($default_site_user) {
      elseif (user_access('administer salesforce')) {
        drupal_set_message(t('Your Salesforce account password expired.  Please
         <a href="">login to</a> and
         change your password.'), 'error');
      else {
        salesforce_api_log(SALESFORCE_LOG_SOME, 'Connection to Salesforce
          due to expired password for @user.', array(
          '@user' => $username,
        ), WATCHDOG_ERROR);
  else {
    salesforce_api_log(SALESFORCE_LOG_SOME, 'Connection to Salesforce failed.', array(), WATCHDOG_ERROR);

    // Or return FALSE to indicate the failure.
    $sf = FALSE;
  return $sf;