function sf_entity_export in Salesforce Suite 7
Same name and namespace in other branches
- 7.2 sf_entity/sf_entity.module \sf_entity_export()
Exports an entity to Salesforce using the specified fieldmap and stores the ID of the Salesforce object for the entity.
Parameters
(object) $entity: The entity to export
$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_entity_export()
- sf_entity_salesforce_form_submit in sf_entity/
sf_entity.module - sf_entity_save in sf_entity/
sf_entity.module
File
- sf_entity/
sf_entity.module, line 688 - Integrates fieldable entities with the Salesforce API.
Code
function sf_entity_export($entity, $fieldmap, $sfid = NULL) {
// Attempt to connect to Salesforce.
$sf = salesforce_api_connect();
if (!$sf) {
drupal_set_message(t('Unable to connect to Salesforce using <a href="!url">current credentials</a>.', array(
'!url' => url(SALESFORCE_PATH_ADMIN),
)));
return FALSE;
}
if (empty($entity)) {
DrupalSalesforce::watchdog(SALESFORCE_LOG_SOME, 'sf_entity_export was provided an empty entity to export: ' . print_r($entity, 1), array(), WATCHDOG_ERROR);
return FALSE;
}
// Load the fieldmap so we can get the object name.
$map = salesforce_api_fieldmap_load($fieldmap);
list($id, $vid, $bundle) = entity_extract_ids($map['drupal_entity'], $entity);
// 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', $entity, $fieldmap);
if (!empty($matches)) {
$sfid = reset($matches);
}
$entity->salesforce['sfid'] = $sfid;
}
// Create an object for export based on the specified fieldmap.
$object = salesforce_api_fieldmap_export_create($fieldmap, $entity);
$object->Id = $sfid;
// TODO: Salesforce's PHP Toolkit's implementation of "upsert" for Enterprise client
// is BROKEN -- it always tries to update "Contact" instead of accepting a "type"
// parameter. Until it is fixed, we have to update/create.
//
// Upsert creates a new object if object->Id is empty, otherwise updates an existing object.
// $response = $sf->client->upsert('Id', array($object));
if (empty($object->Id)) {
$response = $sf->client
->create(array(
$object,
), $map['salesforce']);
}
else {
$response = $sf->client
->update(array(
$object,
), $map['salesforce']);
}
// If the export was successful...
if ($response->success) {
if (empty($entity->salesforce['sfid'])) {
// Store the Salesforce ID for the node and return TRUE.
salesforce_api_id_save($map['drupal_entity'], $map['drupal_bundle'], $id, $response->id, $fieldmap);
}
return TRUE;
}
else {
// Otherwise log the error and return FALSE.
if (user_access('administer salesforce')) {
if (function_exists('dpm')) {
dpm($response);
}
else {
drupal_set_message(check_plain(print_r($response, 1)), 'error');
}
}
DrupalSalesforce::watchdog(SALESFORCE_LOG_SOME, 'Salesforce returned an unsuccessful response: ' . print_r($response, 1), array(), WATCHDOG_ERROR, l('entity ' . $id, entity_uri($map['drupal_entity'], $entity)));
return FALSE;
}
}