shubinmi/salesforce-bulk-api

Client for Salesforce Bulk Api. Easy way to manipulate your Salesforce data.

1.2.1 2021-09-03 11:51 UTC

This package is not auto-updated.

Last update: 2024-10-26 03:05:17 UTC


README

Easy way to manipulate your Salesforce data

Don't worry, Be happy

Scrutinizer Code Quality Latest Stable Version Maintainability Open Source Love MIT Licence Build Status Codacy Badge

Features

  • INSERT job
  • UPDATE job
  • UPSERT job
  • DELETE job
  • QUERY (SELECT) job

For example see to the tests

Installation

Install the latest version with

$ composer require shubinmi/salesforce-bulk-api

Basic Usage

<?php

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' => 'new@user.net',
            'First_Name__c' => 'New Net'
        ],
        [
            'Email__c' => 'new@user.org',
            'First_Name__c' => 'New Org'
        ],
    ],
    [ // Batch 2
        [
            'Email__c' => 'new1@user.net',
            'First_Name__c' => 'New1 Net'
        ],
        [
            'Email__c' => 'new1@user.org',
            'First_Name__c' => 'New1 Org'
        ],
    ],
    [ // Batch 3
        [
            'Email__c' => 'new2@user.net',
            'First_Name__c' => 'New2 Net'
        ],
        [
            'Email__c' => 'new2@user.org',
            '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;
        }
    }
}

Contribute safely

$ sh ./vendor/phpunit/phpunit/phpunit ./tests/services