Below is an example of a php's array_push that can increase the efficiency of insert, update, or upsert script which run against Salesforce's API. You can create a variable for the number of records:
PHP Code:
$number_records = 200;
Here is a look at the entire code
PHP Code:
<?php
// Salesforce Login information
$wsdl = './includes/soapclient/partner.wsdl.xml';
$userName = "myemail@email.com";
$password = "salesforce password";
// Process of logging on and getting a salesforce.com session
$client = new SforcePartnerClient();
$client->createConnection($wsdl);
$loginResult = $client->login($userName, $password);
// set the amount of records to process at one time
// 200 is the max amount of records that can be processed at one time
$number_records = 200;
$record_count = $number_records;
// will start at the first record, then will increment by $number_records each iteration
$starting_record = 1;
while ($record_count == $number_records)
{
while ($row = $recordSet->FetchRow())
{
// array_map cleans the data and changes special characters that maybe coming from
// Your data source to valid XML formatting (e.g & > &) which will allow it to
// upload successfully to Salesforce
$row = array_map('htmlspecialchars', array_map('stripslashes', $row));
// moves your data to your new array
$all_fields[$i]['ID'] = $row['ID'];
$i++;
}
$record_count = $i;
$sObjects = array(); //setup another array for the array_push
// this foreach loop moves data from $all_fields into a new variable called
// $fieldset and then pushes it to your new object > $sObject
foreach ($all_fields as $fieldset)
{
$sObject = new sObject();
$sObject->type = 'PricebookEntry';
$sObject->fields = $fieldset;
// This is where the array_push is done
// This pushes the 200 records that are in $sOobject into $sObjects
// which creates your multidimensional array with your data
array_push($sObjects, $sObject);
}
/* You can uncomment these two lines to view your data prior to it being sent to salesforce
and check the fields to make sure that they are mapping correctly */
//echo '<pre>' . print_r($sObjects, true) . '</pre>';
//exit;
// sends all 200 records in this batch and login information to salesforce
// class to process against your instance of Salesforce
$success = your_function($client, $sObjects);
}
/* Salesforce Functions */
function your_function($client, $sObjects)
{
$file_updated = 0;
$file_failed = 0;
try
{
// The update process that sends all 200 records
// to your instance and returns the response records
// into a variable called $results
$results = $client->update($sObjects);
$k = 0;
// This loop processes $result to build the log files
foreach ($results as $result)
{
// Build string from fields in $sObjects array
// At this point, the record has already been upserted
// We just need the data for the log file
// The string is the same, regardless of the result
if ($result->success)
{
$data2 = $result->id;
$file_updated++;
$file_created = "./files/yourfile_updated.txt";
file_put_contents($file_created, $data2 . "\n", FILE_APPEND);
}
else
{
$file_failed++;
// The errors object also contains fields and status_code
// There are many differnt types of error messages, you
// must consult Salesforce's API documentation for the
// messages
$errMessage = $result->errors->message;
$file_failed = "./files/yourfile_failed.csv";
file_put_contents($file_failed, $errMessage . "\n", FILE_APPEND);
}
$k++;
}
// Put the result counts into an array to pass back as the result.
$success = array();
array_push($success, $file_updated, $file_failed);
return $success;
exit;
}
catch (exception $e)
{
// This is reached if there is a major problem in the data or with
// the salesforce.com connection. Normal data errors are caught by
// salesforce.com
echo '<pre>' . print_r($e, true) . '</pre>';
return false;
exit;
}
}
?>
If you have any comments, questions, or improvements that can be made to this code or others posted in this community, please do register and let me know
Bookmarks