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) {
if (isset($entity->sf_entity_skip_export)) {
return;
}
foreach (module_implements('salesforce_api_pre_save') as $module) {
$function = $module . '_salesforce_api_pre_save';
$continue = $function($entity, $type, $op);
if ($continue === FALSE) {
return;
}
}
$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 (!empty($salesforce->name)) {
$map = salesforce_api_salesforce_fieldmap_load($salesforce->name);
}
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 (empty($maps)) {
return;
}
$map = reset($maps);
$salesforce->name = $map->name;
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');
}
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);
}
}