Salesforce Integration | Upsert multiple records with single connection in PHP

| By Webner

When we create connection with “SforceEnterpriseClient”, we can upsert one record and then we need to recreate connection for next upsert, which eventually can result in the exception:

“UnexpectedErrorFault: REQUEST_LIMIT_EXCEEDED: TotalRequests Limit exceeded.”

The solution is to use “SforcePartnerClient” instead of “SforceEnterpriseClient”.

An example with steps to upsert records in bulk:

1. Include these files in your php code:

require_once ('SFDCPHPtoolkit/soapclient/SforcePartnerClient.php');
require_once ('SFDCPHPtoolkit/soapclient/SforceHeaderOptions.php');

2. Create an array of fields:

$i = 0;
foreach (...........)
$all_fields[$i]['Name'] = $fileName;							$all_fields[$i]['WebnerSYS__Account_id__c'] =$sfId;
$all_fields[$i]['WebnerSYS__Description__c'] = htmlspecialchars($description);
$all_fields[$i]['WebnerSYS__S3_File_Url__c'] =$s3URL;
$all_fields[$i]['WebnerSYS__DocExternalId__c'] =$key;

3. Create another array as $sObjects. Create a new object of sObject() class which converts the previous array to object of sObject() class and push into $sObjects:

$sObjects = array();
foreach ($all_fields as $fieldset)
$sObject = new sObject();
$sObject->type = 'WebnerSYS__Documents__c'; // Salesforce Table or object that you will perform the upsert on
$sObject->fields = $fieldset;
array_push($sObjects, $sObject);

4. Create the connection using “SforcePartnerClient”:

$wsdl = 'SFDCPHPtoolkit/soapclient/partner.wsdl.xml';
$userName = "USERNAME";
// Process of logging on and getting a session
$client = new SforcePartnerClient();
$loginResult = $client->login($userName, $password);

5. Upsert command:

$results = $client->upsert("WebnerSYS__DocExternalId__c", $sObjects); //WebnerSYS__DocExternalId__c is an external ID

Leave a Reply

Your email address will not be published. Required fields are marked *