function sf_entity_save in Salesforce Suite 7.2
Same name and namespace in other branches
- 7 sf_entity/sf_entity.module \sf_entity_save()
2 calls to sf_entity_save()
- sf_entity_entity_insert in sf_entity/
sf_entity.module - Implements hook_entity_insert(). Exports an entity on initial save if the fieldmap is configured accordingly.
- sf_entity_entity_update in sf_entity/
sf_entity.module - Implements hook_entity_update(). Exports an entity on update if the fieldmap is configured accordingly.
File
- sf_entity/
sf_entity.module, line 141 - Integrates fieldable entities with the Salesforce API.
Code
function sf_entity_save($entity, $type, $op) {
// When importing *from* Salesforce, don't export *back* to Salesforce.
if (isset($entity->sf_entity_skip_export)) {
return;
}
// Allow modules to alter the Salesforce object and fieldmap prior to saving,
// or to claim it for queue processing.
foreach (module_implements('salesforce_api_pre_save') as $module) {
$function = $module . '_salesforce_api_pre_save';
$continue = $function($entity, $type, $op);
if ($continue === FALSE) {
return;
}
}
// If this is an update, and the node already has a Salesforce mapping,
// try to load it. If the load fails, we need to fetch the appropriate
// fieldmap. Either way, we're upserting the Salesforce record.
$salesforce = (object) array(
'name' => NULL,
'sfid' => NULL,
);
list($oid, $vid, $bundle) = entity_extract_ids($type, $entity);
if ($oid) {
$salesforce = salesforce_api_id_load($oid, $type, $bundle);
}
// If we have an existing link, attempt to load the associated map.
if (!empty($salesforce->name)) {
$map = salesforce_api_salesforce_fieldmap_load($salesforce->name);
}
// If the Salesforce link wasn't found, or if it was found but associated to a
// non-existent map, load any maps associated with this entity type.
// @todo: Determine why this isn't loading maps that should be there.
if (empty($salesforce->name) || empty($map)) {
$maps = salesforce_api_salesforce_fieldmap_load_by(array(
'drupal_entity' => $type,
'drupal_bundle' => $bundle,
));
if (empty($maps)) {
return;
}
}
else {
$maps = array(
$map->name => $map,
);
}
foreach ($maps as $map) {
$auto_create = $map->automatic & SALESFORCE_AUTO_SYNC_CREATE;
$auto_update = $map->automatic & SALESFORCE_AUTO_SYNC_UPDATE;
if (!$auto_create && $op == 'insert' || !$auto_update && $op == 'update') {
unset($maps[$map->name]);
}
}
// If all our maps were unset, abort this procedure.
if (empty($maps)) {
return;
}
// Otherwise, use the first fieldmap.
$map = reset($maps);
$salesforce->name = $map->name;
// Check if there is more than one fieldmap in the result.
// @todo: Is it necessary to use AND instead of &&
if (user_access('administer salesforce') and next($maps)) {
if (!empty($map->description)) {
$description = '(' . $map->description . ')';
}
drupal_set_message(t('Warning: more than one "automatic" salesforce mapping detected. Used fieldmap !map_name @map_description.', array(
'!map_name' => l($map->name, SALESFORCE_PATH_FIELDMAPS . '/' . $map->name . '/edit'),
'@map_description' => $description,
)), 'warning');
}
// Finally, export the entity to Salesforce.
try {
sf_entity_export($entity, $salesforce->name, $salesforce->sfid);
} catch (Exception $e) {
$uri = entity_uri($type, $entity);
$link = NULL;
if (isset($uri['path'])) {
$link = l('view', $uri['path']);
}
salesforce_api_log(SALESFORCE_LOG_SOME, t('Exception while attempting to export entity: <pre>%e</pre>'), array(
'%e' => $e
->getMessage(),
), WATCHDOG_ERROR, $link);
}
}