# README

Bulk 2.0 API

back

The bulk package is an implementation of Salesforce APIs centered on Bulk 2.0 operations. These operations include:

  • Creating a job
  • Upload job data
  • Close or Abort a job
  • Delete a job
  • Get all jobs
  • Get job info
  • Get job successful records
  • Get job failed records
  • Get job unprocessed records

As a reference, see Salesforce API documentation

Examples

The following are examples to access the APIs. It is assumed that a sfdc session has been created.

Creating a Job

	resource, err := bulk.NewResource(session)
	if err != nil {
		fmt.Printf("Bulk Resource Error %s\n", err.Error())
		return
	}

	jobOpts := bulk.Options{
		ColumnDelimiter: bulk.Pipe,
		Operation:       bulk.Insert,
		Object:          "Account",
	}
	job, err := resource.CreateJob(jobOpts)
	if err != nil {
		fmt.Printf("Job Create Error %s\n", err.Error())
		return
	}

Uploading Job Data

	fields := []string{
		"Name",
		"FirstName__c",
		"LastName__c",
		"Site",
	}
	formatter, err := bulk.NewFormatter(job, fields)
	if err != nil {
		fmt.Printf("Formatter Error %s\n", err.Error())
		return
	}

	failedRecord := &bulkRecord{
		fields: map[string]interface{}{
			"FirstName__c": "TallFailing",
			"LastName__c":  "PersonFailing",
			"Site":         "MySite",
		},
	}
	successRecord := &bulkRecord{
		fields: map[string]interface{}{
			"Name":         "Tall Person Success",
			"FirstName__c": "TallSuccess",
			"LastName__c":  "PersonSuccess",
			"Site":         "MySite",
		},
	}
	err = formatter.Add(failedRecord, successRecord)
	if err != nil {
		fmt.Printf("Formatter Record Error %s\n", err.Error())
		return
	}

	err = job.Upload(formatter.Reader())
	if err != nil {
		fmt.Printf("Job Upload Error %s\n", err.Error())
		return
	}

Close or Abort Job

	response, err := job.Close()
	if err != nil {
		fmt.Printf("Job Info Error %s\n", err.Error())
		return
	}
	fmt.Println("Bulk Job Closed")
	fmt.Println("-------------------")
	fmt.Printf("%+v\n", response)

Delete a Job

	err := job.Delete()
	if err != nil {
		fmt.Printf("Job Delete Error %s\n", err.Error())
		return
	}

Get All Jobs

	parameters := bulk.Parameters{
		IsPkChunkingEnabled: false,
		JobType:             bulk.V2Ingest,
	}
	jobs, err := resource.AllJobs(parameters)
	if err != nil {
		fmt.Printf("All Jobs Error %s\n", err.Error())
		return
	}
	fmt.Println("All Jobs")
	fmt.Println("-------------------")
	fmt.Printf("%+v\n\n", jobs)

Get Job Info

	info, err := job.Info()
	if err != nil {
		fmt.Printf("Job Info Error %s\n", err.Error())
		return
	}
	fmt.Println("Bulk Job Information")
	fmt.Println("-------------------")
	fmt.Printf("%+v\n", info)

Get Job Successful Records

	info, err = job.Info()
	if err != nil {
		fmt.Printf("Job Info Error %s\n", err.Error())
		return
	}

	if (info.NumberRecordsProcessed - info.NumberRecordsFailed) > 0 {
		successRecords, err := job.SuccessfulRecords()
		if err != nil {
			fmt.Printf("Job Success Records Error %s\n", err.Error())
			return
		}
		fmt.Println("Successful Record(s)")
		fmt.Println("-------------------")
		for _, successRecord := range successRecords {
			fmt.Printf("%+v\n\n", successRecord)
		}

	}

Get Job Failed Records

	info, err = job.Info()
	if err != nil {
		fmt.Printf("Job Info Error %s\n", err.Error())
		return
	}

	if info.NumberRecordsFailed > 0 {
		failedRecords, err := job.FailedRecords()
		if err != nil {
			fmt.Printf("Job Failed Records Error %s\n", err.Error())
			return
		}
		fmt.Println("Failed Record(s)")
		fmt.Println("-------------------")
		for _, failedRecord := range failedRecords {
			fmt.Printf("%+v\n\n", failedRecord)
		}
	}

Get Job Unprocessed Records

	info, err = job.Info()
	if err != nil {
		fmt.Printf("Job Info Error %s\n", err.Error())
		return
	}

	unprocessedRecords, err := job.UnprocessedRecords()
	if err != nil {
		fmt.Printf("Job Unprocessed Records Error %s\n", err.Error())
		return
	}
	fmt.Println("Unprocessed Record(s)")
	fmt.Println("-------------------")
	for _, unprocessedRecord := range unprocessedRecords {
		fmt.Printf("%+v\n\n", unprocessedRecord)
	}

# Functions

NewFormatter creates a new formatter using the job and the list of fields.
NewResource creates a new bulk 2.0 REST resource.

# Constants

Aborted the job has been aborted.
Backquote is the (`) character.
BigObjects is the big objects job.
Caret is the (^) character.
CarriageReturnLinefeed is the (\r\n) character.
Classic is the bulk job 1.0.
Comma is the (,) character.
CSV is the supported content data type.
Delete is the object operation for deleting records.
Failed some records in the job failed.
Insert is the object operation for inserting records.
JobComplete the job was processed by Salesforce.
Linefeed is the (\n) character.
Open the job has been created and job data can be uploaded tothe job.
Pipe is the (|) character.
SemiColon is the (;) character.
Tab is the (\t) character.
Update is the object operation for updating records.
UpdateComplete all data for the job has been uploaded and the job is ready to be queued and processed.
Upsert is the object operation for upserting records.
V2Ingest is the bulk job 2.0.

# Structs

FailedRecord indicates why the record failed and the data of the record.
Formatter is the object that will add records for the bulk uploader.
Info is the response to the job information API.
Job is the bulk job.
JobRecord is the record for the job.
Jobs presents the response from the all jobs request.
Options are the options for the job.
Parameters to query all of the bulk jobs.
Resource is the structure that can be used to create bulk 2.0 jobs.
Response is the response to job APIs.
SuccessfulRecord indicates for the record was created and the data that was uploaded.
UnprocessedRecord is the unprocessed records from the job.

# Interfaces

Record is the interface to the fields of the bulk uploader record.

# Type aliases

ColumnDelimiter is the column delimiter used for CSV job data.
ContentType is the format of the data being processed.
JobType is the bulk job type.
LineEnding is the line ending used for the CSV job data.
Operation is the processing operation for the job.
State is the current state of processing for the job.