function sf_node_export in Salesforce Suite 6.2
Same name and namespace in other branches
- 5.2 sf_node/sf_node.module \sf_node_export()
Exports a node to Salesforce using the specified fieldmap and stores the ID of the Salesforce object for the node.
Parameters
$node: The node object to export (also accepts a numeric node id).
$fieldmap: The index of the fieldmap to use to create the export object.
$sfid: The Salesforce ID of the object you want to update. If left NULL, a new object will be created at Salesforce.
Return value
TRUE or FALSE indicating the success of the operation.
2 calls to sf_node_export()
- sf_node_nodeapi in sf_node/
sf_node.module - Implementation of hook_nodeapi().
- sf_node_salesforce_form_submit in sf_node/
sf_node.module
File
- sf_node/
sf_node.module, line 655 - Integrates the core node object and various node related modules with the Salesforce API.
Code
function sf_node_export($node, $name, $sfid = NULL) {
// Attempt to connect to Salesforce.
$sf = salesforce_api_connect();
if (!$sf) {
// Let modules react to a failure to export this node.
module_invoke_all('salesforce_api_export_connect_fail', is_object($node) ? $node->nid : $node, $name, $sfid);
if (user_access('administer salesforce')) {
drupal_set_message(t('Unable to connect to Salesforce using <a href="!url">current credentials</a>.', array(
'!url' => url(SALESFORCE_PATH_ADMIN),
)));
}
return FALSE;
}
// Load the node if we didn't get a full object.
if (is_numeric($node)) {
$node = node_load($node);
}
// Load the fieldmap so we can get the object name.
$map = salesforce_api_fieldmap_load($name);
// Look for any matching records which we might want to update instead of creating duplicates.
if (empty($sfid)) {
$matches = salesforce_api_search_for_duplicates('export', 'node', $node, $name);
if (!empty($matches)) {
$sfid = reset($matches);
$node->salesforce = array(
'sfid' => $sfid,
'name' => $name,
);
salesforce_api_id_save('node', $node->nid, $sfid, $name, 'export');
$node = node_load($node->nid, NULL, TRUE);
}
if (empty($node)) {
$node = (object) array();
}
}
// Create an object for export based on the specified fieldmap.
$object = salesforce_api_fieldmap_export_create($name, $node);
$object->Id = $sfid;
foreach (module_implements('salesforce_api_pre_export') as $module) {
$function = $module . '_salesforce_api_pre_export';
$continue = $function($object, $map, $node->nid);
if ($continue === FALSE) {
return;
}
}
// If any modules altered the fieldmap, ensure that the new fieldmap name
// is used for the rest of the export.
if ($map->name != $name) {
$name = $map->name;
}
try {
$response = $sf->client
->upsert('Id', array(
$object,
), $map->salesforce);
} catch (Exception $e) {
salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception while attempting to upsert node: %msg <pre>%e</pre>', array(
'%msg' => $e
->getMessage(),
'%e' => print_r($e, TRUE),
), WATCHDOG_ERROR, l('node ' . $node->nid, 'node/' . $node->nid));
}
// Check to see if response is an array of objects
if (is_array($response) && count($response) > 0) {
$response = $response[0];
}
// If we got errors, handle them before proceeding
if (is_object($response->errors)) {
// If we got "Entity is deleted" and we're configured to unlink and upsert,
// do it.
if ($response->errors->statusCode == 'ENTITY_IS_DELETED' && ($response->errors->fields == 'Id' || empty($response->errors->fields)) && SALESFORCE_DELETED_POLICY_UPSERT == variable_get('salesforce_api_entity_deleted_policy', SALESFORCE_DELETED_POLICY_UPSERT)) {
// If the entity is deleted, unlink ALL the linked drupal objects.
salesforce_api_id_unlink(array(
'sfid' => $object->Id,
));
$node->salesforce->sfid = $object->Id = NULL;
// Look for any matching records which we might want to update instead of
// creating duplicates. Assume that salesforce_api_search_for_duplicates()
// will never return a deleted record.
$matches = salesforce_api_search_for_duplicates('export', 'node', $node, $name);
if (!empty($matches)) {
$sfid = reset($matches);
$node->salesforce->sfid = $sfid;
}
salesforce_api_log(SALESFORCE_LOG_SOME, 'Salesforce entity deleted. Attempting to unlink and upsert. <pre>%response</pre>', array(
'%response' => print_r($response, 1),
), WATCHDOG_ERROR, l('node ' . $node->nid, 'node/' . $node->nid));
try {
$response = $sf->client
->upsert('Id', array(
$object,
), $map->salesforce);
} catch (Exception $e) {
salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception while attempting to upsert node: %msg <pre>%e</pre>', array(
'%msg' => $e
->getMessage(),
'%e' => print_r($e, TRUE),
), WATCHDOG_ERROR, l('node ' . $node->nid, 'node/' . $node->nid));
}
}
}
// If the export was successful...
if ($response->success) {
// Store the Salesforce ID for the node and return TRUE.
salesforce_api_id_save('node', $node->nid, $response->id, $name, 'export');
}
else {
// Otherwise log the error and return FALSE.
if (user_access('administer salesforce')) {
if (function_exists('dpm')) {
dpm($response);
}
else {
drupal_set_message(t('Salesforce Suite export error: <pre>%response</pre>', array(
'%response' => print_r($response, 1),
)), 'error');
}
}
salesforce_api_log(SALESFORCE_LOG_SOME, 'Salesforce returned an unsuccessful response: <pre>%response</pre>', array(
'%response' => print_r($response, 1),
), WATCHDOG_ERROR, l('node ' . $node->nid, 'node/' . $node->nid));
}
module_invoke_all('salesforce_api_post_export', $object, $map, $node->nid, $response);
return $response->success;
}