Giter VIP home page Giter VIP logo

salesforce-bulkv2-java's Introduction

Java client library for Salesforce Bulk API 2.0

A simple java wrapper for Salesforce Bulk API 2.0

Requirements

Java 8 or later.

Usage

Create client

Bulk2Client client = new Bulk2ClientBuilder()
        .withPassword("<consumer key>", "<consumer secret>", "<username>", "<password>"
        .build();

If you’re verifying authentication on a sandbox organization, simply add useSandbox.

Bulk2Client client = new Bulk2ClientBuilder()
        .withPassword("<consumer key>", "<consumer secret>", "<username>", "<password>"
        .useSandbox()
        .build();

Upload CSV data using a separate request

CreateJobResponse createJobResponse = client.createJob("Account", OperationEnum.INSERT);
String jobId = createJobResponse.getId();

String csv = "Name,Description,NumberOfEmployees\n" +
        "TestAccount1,Description of TestAccount1,30\n" +
        "TestAccount2,Another description,40\n" +
        "TestAccount3,Yet another description,50";
client.uploadJobData(jobId, csv);

// When using a separate request to upload data, make sure to close the job
JobInfo closeJobResponse = client.closeJob(jobId);

while (true) {
    TimeUnit.SECONDS.sleep(1);

    GetJobInfoResponse jobInfo = client.getJobInfo(jobId);
    if (jobInfo.isFinished()) {
        break;
    }
}

Upload CSV data using a multipart request

For data sets under 20,000 characters, you can upload the data as part of a multipart request when you create the job.

String csv = "Name,Description,NumberOfEmployees\n" +
        "TestAccount1,Description of TestAccount1,30\n" +
        "TestAccount2,Another description,40\n" +
        "TestAccount3,Yet another description,50";

CreateJobResponse createJobResponse = client.createJob("Account", OperationEnum.INSERT,
        request -> request.withContent(csv));
String jobId = createJobResponse.getId();

while (true) {
    TimeUnit.SECONDS.sleep(1);

    GetJobInfoResponse jobInfo = client.getJobInfo(jobId);
    if (jobInfo.isFinished()) {
        break;
    }
}

Retrieves all jobs

GetAllJobsResponse jobs = client.getAllJobs(request -> request.withJobType(JobTypeEnum.BULK_API_2_0));
for (JobInfo jobInfo : jobs.getRecords()) {
    System.out.println(jobInfo);
}

Retrieve the results of the completed/failed/unprocessed job

Following API returns the results as java.io.Reader

  • Get Job Successful Record Results ( Bulk2Client#getJobSuccessfulRecordResults )
  • Get Job Failed Record Results ( Bulk2Client#getJobFailedRecordResults )
  • Get Job Unprocessed Record Results ( Bulk2Client#getJobUnprocessedRecordResults )
try (BufferedReader reader = new BufferedReader(client.getJobSuccessfulRecordResults(jobId))) {
    reader.lines().forEach(System.out::println);
}

Build

$ ./gradlew build

salesforce-bulkv2-java's People

Contributors

tq-jappy avatar

Watchers

James Cloos avatar Andrzej Cieślak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.