You are here

function sf_node_import in Salesforce Suite 5.2

Same name and namespace in other branches
  1. 6.2 sf_node/sf_node.module \sf_node_import()

Imports data from Salesforce into a node.

Parameters

$sfid: The Salesforce ID of the object from which you want to import.

$fieldmap: The index of the fieldmap to use to create the export object.

$nid: The nid of the node to update. If left NULL, a new node will be created.

Return value

The nid of the imported node or FALSE on failure.

1 call to sf_node_import()
sf_node_salesforce_form_submit in sf_node/sf_node.module

File

sf_node/sf_node.module, line 460
Integrates the core node object and various node related modules with the SalesForce API.

Code

function sf_node_import($sfid, $fieldmap, $nid = NULL) {

  // Retrieve the object from Salesforce.
  $sf = salesforce_api_connect();
  $data = $sf
    ->retrieve(array(
    $sfid,
  ), $fieldmap);

  // Return FALSE if the object data was not found at Salesforce.
  if (empty($data)) {
    return FALSE;
  }

  // Load the fieldmap data.
  $map = salesforce_api_fieldmap_load($fieldmap);

  // Load the object definitions.
  $drupal_object = salesforce_api_fieldmap_objects_load('drupal', $map['drupal']);
  $salesforce_object = salesforce_api_fieldmap_objects_load('salesforce', $map['salesforce']);

  // If a node was specified, attempt to load it.
  $node = node_load($nid);

  // If the node exists, simply update the existing node.
  if ($node->nid) {

    // Loop through the fields on the fieldmap.
    foreach ($map['fields'] as $value => $key) {

      // If a handler is specified for importing a value from Salesforce.
      if (isset($drupal_object['fields'][$key]['import'])) {

        // Get the value for the field from the handler function.
        $drupal_object['fields'][$key]['import']($node, $key, $data, $value);
      }
      elseif (isset($data->{$value})) {

        // Otherwise set the field on the export object to the value of the source
        // field if it's present on the source object.
        $node->{$key} = $data->{$value};
      }
    }
    node_save($node);
  }
  return $node->nid;
}