You are here

function salesforce_api_id_save in Salesforce Suite 7.2

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

Saves the Salesforce ID and fieldmap index of a Drupal object. Also stores the timestamp of creation for the object mapping, and when the object was last exported to Salesforce or imported to Drupal.

@todo salesforce_api_id_save_multi()

Parameters

int $oid: The associated unique ID used to identify the object in Drupal.

string $sfid: The Salesforce ID of the associated object in the Salesforce database.

string $name: The name of the fieldmap used to generate the export.

string $entity_name: The type of Drupal entity being saved.

string $bundle_name: The Drupal bundle type being saved.

string $op_type: The type of operation being performed. Possible values are 'import', 'export', and 'link'.

Return value

TRUE if was successful in saving the link, FALSE otherwise.

3 calls to salesforce_api_id_save()
sf_entity_export in sf_entity/sf_entity.module
Exports an entity to Salesforce using the specified fieldmap and stores the ID of the Salesforce object for the entity.
sf_entity_import in sf_entity/sf_entity.module
Imports data from Salesforce into a Drupal entity
sf_entity_salesforce_form_submit in sf_entity/sf_entity.module
Form submit handler for the [entity-name]/%/salesforce form.

File

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

Code

function salesforce_api_id_save($oid = NULL, $sfid = NULL, $name = NULL, $entity_name = NULL, $bundle_name = NULL, $op_type = NULL) {

  // Allows other modules to respond to salesforce_api_id_save being called.
  foreach (module_implements('salesforce_api_id_save_alter') as $module) {
    $function = $module . '_salesforce_api_id_save_alter';
    $continue = $function($oid, $sfid, $name, $entity_name, $bundle_name, $op_type);
    if ($continue === FALSE) {
      return FALSE;
    }
  }
  if ($oid) {
    $oid = (int) $oid;
  }
  if ($oid && $sfid && $name && $entity_name && $bundle_name && $op_type) {
    $op_type == 'export' ? $fieldname = 'last_export' : ($fieldname = 'last_import');
    $result = db_merge('salesforce_object_map')
      ->key(array(
      'oid' => $oid,
      'name' => $name,
    ))
      ->insertFields(array(
      'oid' => $oid,
      'sfid' => $sfid,
      'name' => $name,
      'drupal_entity' => $entity_name,
      'drupal_bundle' => $bundle_name,
      'created' => REQUEST_TIME,
      $fieldname => REQUEST_TIME,
    ))
      ->updateFields(array(
      $fieldname => REQUEST_TIME,
    ))
      ->execute();

    // Log if an insert has been performed.
    if ($result == MergeQuery::STATUS_INSERT) {
      salesforce_api_log(SALESFORCE_LOG_ALL, 'On !op, successfully linked Drupal !entity : !bundle !oid to Salesforce ID !sfid with fieldmap !name', array(
        '!op' => $op_type,
        '!entity' => $entity_name,
        '!bundle' => $bundle_name,
        '!oid' => $oid,
        '!sfid' => $sfid,
        '!name' => $name,
      ));
      return TRUE;
    }
    elseif ($result == MergeQuery::STATUS_UPDATE) {
      salesforce_api_log(SALESFORCE_LOG_ALL, 'On !op, successfully re-saved linkage between Drupal !entity : !bundle !oid to Salesforce ID !sfid with fieldmap !name', array(
        '!op' => $op_type,
        '!entity' => $entity_name,
        '!bundle' => $bundle_name,
        '!oid' => $oid,
        '!sfid' => $sfid,
        '!name' => $name,
      ));
      return TRUE;
    }
    else {
      salesforce_api_log(SALESFORCE_LOG_ALL, 'On !op, failed to link Drupal !entity : !bundle !oid to Salesforce ID !sfid with fieldmap !name', array(
        '!op' => $op_type,
        '!entity' => $entity_name,
        '!bundle' => $bundle_name,
        '!oid' => $oid,
        '!sfid' => $sfid,
        '!name' => $name,
      ), WATCHDOG_ERROR);
    }
  }
  else {
    salesforce_api_log(SALESFORCE_LOG_ALL, 'Attempted to save Drupal->Salesforce linkage with insufficient information. Drupal entity: !entity, Drupal bundle: !bundle, Drupal entity id: !oid, Salesforce ID: !sfid, Fieldmap: !name, Operation: !op', array(
      '!entity' => $entity_name,
      '!bundle' => $bundle_name,
      '!oid' => $oid,
      '!sfid' => $sfid,
      '!name' => $name,
      '!op' => $op_type,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
}