PHP code example of sharmasabhay / salesforce-bulk-api

1. Go to this page and download the library: Download sharmasabhay/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/ */

    

sharmasabhay / 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');

// Set up Insert SF job
$jobRequest = (new CreateJobDto)
    ->setObject('My_User__c')
    ->setOperation(CreateJobDto::OPERATION_INSERT);

// Data for Insert to custom SF entity
$data1 = [
    [
        'Email__c' => '[email protected]',
        'First_Name__c' => 'New Net'
    ],
    [
        'Email__c' => '[email protected]',
        'First_Name__c' => 'New Org'
    ]
];
$data2 = [
    [
        'Email__c' => '[email protected]',
        'First_Name__c' => 'New1 Net'
    ],
    [
        'Email__c' => '[email protected]',
        'First_Name__c' => 'New1 Org'
    ]
];

// Init Insert job and pull data
$insertJob = (new JobSFApiService($params))
    ->initJob($jobRequest)
    ->addBatchToJob($data1)
    ->addBatchToJob($data2)
    ->closeJob();

// Set up params for Upsert SF job
$jobRequest
    ->setOperation(CreateJobDto::OPERATION_UPSERT)
    ->setExternalIdFieldName('Email__c');

// Do Upsert job
$upsertJob = new JobSFApiService($params);
$upsertJob->initJob($jobRequest);

$data = [
    [
        'Email__c' => '[email protected]',
        'First_Name__c' => 'Not new Net'
    ],
    [
        'Email__c' => '[email protected]',
        'First_Name__c' => 'New Com'
    ]
];
$upsertJob
    ->addBatchToJob($data)
    ->closeJob();

// Collect jobs errors
$errorsOnInsert = $insertJob->waitingForComplete()->getErrors();
$errorsOnUpsert = $upsertJob->waitingForComplete()->getErrors();

// Operate with errors
foreach ($errorsOnInsert as $error) {
    /** @var SFBatchErrors $error */
    $errorsBatch         = $error->getBatchInfo();
    $errorsMsg           = $error->getErrorMessages();
    $errorsElementNumber = $error->getErrorNumbers();
    if (empty($errorsElementNumber)) {
        echo 'Batch ' . $errorsBatch->getId() . ' return ' . $errorsBatch->getState() . PHP_EOL;
    } else {
        echo 'Batch ' . $errorsBatch->getId() . ' fail for next rows:' . PHP_EOL;
        foreach ($errorsElementNumber as $errorMsgKey => $errorRowNumber) {
            echo 'Row number = ' . $errorRowNumber 
                . ' Error message = ' . $errorsMsg[$errorMsgKey] . PHP_EOL;
        }
    }
}