function salesforce_api_upsert in Salesforce Suite 7.2
Wraps SforceBaseClient::upsert. Upserts a record in Salesforce. If there is an existing record in Salesforce with the same ID, that record is updated. Otherwise, a new record is created.
Parameters
array $records: Either an array of arrays of Salesforce fields, to be converted to sObjects, of the specified type, or else an array of sObjects.
string $type: The type of sObject to update. Must either be a core sObject, or a custom type defined in your WSDL. Custom types all end in "__c". Default type is "Contact".
string $key: The Salesforce field, or external ID, on which to upsert. Default is "Id".
object $sf: A currently-active Salesforce connection. If none is passed in, one will be created.
Return value
array An array containing an array of Salesforce IDs successfully upserted, and the number of failures, or FALSE if no Salesforce connection could be made or an exception was thrown.
File
- salesforce_api/
salesforce_api.module, line 1653 - Defines an API that enables modules to interact with the Salesforce server.
Code
function salesforce_api_upsert(array $records, $type = 'Contact', $key = 'Id', $sf = NULL) {
// Connects to Salesforce if no existing connection supplied.
if (!is_object($sf)) {
$sf = salesforce_api_connect();
// Return FALSE if could not connect to Salesforce.
if ($sf == FALSE) {
return FALSE;
}
}
// Iterates through the records passed in and convert them to objects if necessary.
$i = 0;
foreach ($records as $record) {
if (!is_object($record)) {
$records[$i] = (object) $record;
}
$i++;
}
try {
$results = $sf->client
->upsert($key, $records, $type);
} catch (Exception $e) {
salesforce_api_log(SALESFORCE_LOG_SOME, 'The following exception occurred while attempting to upsert records: <pre>%e</pre>', array(
'%msg' => $e
->getMessage(),
'%e' => print_r($e, TRUE),
), WATCHDOG_ERROR);
return FALSE;
}
// Sets up the variables for the array of information about results of upsert operation.
$success_ids = array();
$failures = 0;
$created_ids = array();
$updated_ids = array();
// $is_deleted_ids = array();
// Iterate over the resultset.
foreach ($results as $result) {
// Handle any errors.
// @todo: Log is_deleted errors separately, so they can be handled by unlink & upsert on
// a successive call to this function.
if (isset($result->errors) && is_array($result->errors)) {
$err_msgs = array();
$status_codes = array();
// Log all errors to watchdog.
// @todo: Present them more nicely than an array with print_r().
foreach ($result->errors as $error) {
$err_msgs[] = $error->message;
$status_codes[] = $error->statusCode;
if ($error->statusCode == 'ENTITY_IS_DELETED') {
// @todo: Figure out a way to determine which one was deleted.
}
}
salesforce_api_log(SALESFORCE_LOG_SOME, 'Errors occurred while attempting to upsert record: <pre>%msgs</pre>', array(
'%msgs' => print_r($err_msgs, TRUE),
'%codes' => print_r($status_codes, TRUE),
), WATCHDOG_ERROR);
// Increment the number of failures.
$failures++;
}
elseif (isset($result->success) && $result->success == TRUE) {
$success_ids[] = $result->id;
// Separates successes into creates and updates.
if (isset($result->created) && $result->created == TRUE) {
$created_ids[] = $result->id;
}
else {
$updated_ids[] = $result->id;
}
}
}
// Return the ids of results, grouped appropriately.
$result_info = array(
'successes' => $success_ids,
'failures' => $failures,
'created' => $created_ids,
'updated' => $updated_ids,
);
return $result_info;
}