PHP code example of shubinmi / salesforce-bulk-api

1. Go to this page and download the library: Download shubinmi/salesforce-bulk-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shubinmi / salesforce-bulk-api example snippets




use SalesforceBulkApi\dto\CreateJobDto;
use SalesforceBulkApi\objects\SFBatchErrors;
use SalesforceBulkApi\conf\LoginParams;
use SalesforceBulkApi\services\JobSFApiService;

// Set up API Client
$params = (new LoginParams)
    ->setUserName('mySFLogin')
    ->setUserPass('MySFPass')
    ->setUserSecretToken('mySecretTokenFomSF');

// (optional) Flag as Sandbox
// $params->setEndpointPrefixAsSandbox();

// Set up SF job
$jobRequest = (new CreateJobDto)
    ->setObject('My_User__c')
    ->setOperation(CreateJobDto::OPERATION_INSERT); // Use CreateJobDto::OPERATION_UPSERT for upsert operation

// (optional if Upsert) Set an External Id
// $upsertKey = 'My_External_Id__c';
// $jobRequest->setExternalIdFieldName($upsertKey);

// Data Batches
$data = [
    [ // Batch 1
        [
            'Email__c' => '[email protected]',
            'First_Name__c' => 'New Net'
        ],
        [
            'Email__c' => '[email protected]',
            'First_Name__c' => 'New Org'
        ],
    ],
    [ // Batch 2
        [
            'Email__c' => '[email protected]',
            'First_Name__c' => 'New1 Net'
        ],
        [
            'Email__c' => '[email protected]',
            'First_Name__c' => 'New1 Org'
        ],
    ],
    [ // Batch 3
        [
            'Email__c' => '[email protected]',
            'First_Name__c' => 'New2 Net'
        ],
        [
            'Email__c' => '[email protected]',
            'First_Name__c' => 'New2 Org'
        ],
    ],
];

// Init Job
$jobService = (new JobSFApiService($params))
    ->initJob($jobRequest);

// Add batches of data, can be up to 10000 records long each
foreach ($data as $batchData) {
    $jobService->addBatchToJob($batchData);
}

// Gather up an ordered list of Batch ids to reference data in the batch, specifically on error handling
// JobSFApiService::waitingForComplete update job statuses in the order returned from Salesforce
// This new order is not necessarily the same order the data was submitted in making referencing the original data difficult
$job = $jobService->getJob();
$batchesInfo = $job->getBatchesInfo();
$batchIdReference = array_flip(array_map(function($batchInfoDto){
    return $batchInfoDto->getId();
}, $batchesInfo));

// Close Job and Wait for Job completion
$jobService
    ->closeJob()
    ->waitingForComplete();

// Collect jobs errors
$errors = $jobService->getErrors();

// Operate with errors
foreach ($errors as $error) {
    
    /** @var SFBatchErrors $error */
    $errorsBatch         = $error->getBatchInfo();
    $batchId             = $errorsBatch->getId();
    $batchNo             = $batchIdReference[$batchId];
    $errorsMsg           = $error->getErrorMessages();
    $errorsElementNumber = $error->getErrorNumbers();

    if (empty($errorsElementNumber)) {
        // No specific errors
        echo "Batch $batchId (#$batchNo) returned a general error" . PHP_EOL;
        echo "\tState: " . $errorsBatch->getState() . ' (' . $errorsBatch->getStateMessage() . ')' . PHP_EOL;
        echo "\tNote: An error here might mean the data types sent are incorrect (eg \"0\" vs 0/false)." . PHP_EOL;
    } else {
        echo "Batch $batchId (#$batchNo) failed for following rows:" . PHP_EOL;
        foreach ($errorsElementNumber as $errorMsgKey => $errorRowNumber) {
            echo "\tRow number = " . $errorRowNumber . " Error message = " . $errorsMsg[$errorMsgKey] . PHP_EOL;
            $record = $data[$batchNo][$errorRowNumber];
            echo "\t\tEmail = " . $record['Email__c'] . PHP_EOL;
            echo "\t\tFirst Name = " .  $record['First_Name__c'] . PHP_EOL;
        }
    }
}