collections

package
v2.0.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2019 License: MIT Imports: 12 Imported by: 0

README

SObject Collection APIs

back

The collection package is an implementation of Salesforce APIs centered on SObject Collection operations. These operations include:

  • Create Multiple Records
  • Update Multiple Records
  • Delete Multiple Records
  • Retrieve Multiple Records

As a reference, see Salesforce API documentation

Examples

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

type dml struct {
	sobject       string
	fields        map[string]interface{}
	id            string
}

func (d *dml) SObject() string {
	return d.sobject
}
func (d *dml) Fields() map[string]interface{} {
	return d.fields
}
func (d *dml) ID() string {
	return d.id
}

type query struct {
	sobject  string
	id       string
	fields   []string
}

func (q *query) SObject() string {
	return q.sobject
}
func (q *query) ID() string {
	return q.id
}
func (q *query) Fields() []string {
	return q.fields
}
Create Multiple Records
// insert some records
var insertRecords []sobject.Inserter

acc1 := &dml{
	sobject: "Account",
	fields: map[string]interface{}{
		"Name":  "Collections Demo",
		"Phone": "9045551212",
	},
}
insertRecords = append(insertRecords, acc1)
acc2 := &dml{
	sobject: "Account",
	fields: map[string]interface{}{
		"Name":      "Collections Demo Two",
		"Active__c": "Yes",
	},
}
insertRecords = append(insertRecords, acc2)
con1 := &dml{
	sobject: "Contact",
	fields: map[string]interface{}{
		"LastName":       "Last Name Demo",
		"AssistantPhone": "6065551212",
	},
}
insertRecords = append(insertRecords, con1)
con2 := &dml{
	sobject: "Contact",
	fields: map[string]interface{}{
		"LastName": "Last Name Demo Two",
		"Level__c": "Tertiary",
	},
}
insertRecords = append(insertRecords, con2)

resource := collections.NewResources(session)
values, err := resource.Insert(true, insertRecords)
if err != nil {
	fmt.Printf("Collection Error %s\n", err.Error())
	return
}

fmt.Println("Collections Inserted")
fmt.Println("-------------------")
for _, value := range values {
	fmt.Printf("%+v\n", value)
}
fmt.Println()
Update Multiple Records
var updateRecords []sobject.Updater

acc1 := &dml{
	sobject: "Account",
	fields: map[string]interface{}{
		"Name":      "Collections Demo Update",
		"Phone":     "4805551212",
		"Active__c": "No",
	},
	id: "0012E00001pXGh2QAG",
}
updateRecords = append(updateRecords, acc1)
acc2 := &dml{
	sobject: "Account",
	fields: map[string]interface{}{
		"Name":      "Collections Demo Two Update",
		"Phone":     "6065551212",
		"Active__c": "Yes",
	},
	id: "0012ExxxAG",
}
updateRecords = append(updateRecords, acc2)
con1 := &dml{
	sobject: "Contact",
	fields: map[string]interface{}{
		"LastName":       "Last Name Demo Update",
		"AssistantPhone": "6065551212",
		"Level__c":       "Tertiary",
	},
	id: "0032ExxxjQAJ",
}
updateRecords = append(updateRecords, con1)
con2 := &dml{
	sobject: "Contact",
	fields: map[string]interface{}{
		"LastName":       "Last Name Demo Two Update",
		"AssistantPhone": "6025551212",
		"Level__c":       "Tertiary",
	},
	id: "0032ExxxkQAJ",
}
updateRecords = append(updateRecords, con2)

resource := collections.NewResources(session)
values, err := resource.Update(true, updateRecords)
if err != nil {
	fmt.Printf("Collection Error %s", err.Error())
	fmt.Println()
	return
}

fmt.Println("Collections Updated")
fmt.Println("-------------------")
for _, value := range values {
	fmt.Printf("%+v\n", value)
}
fmt.Println()
Delete Multiple Records
deleteRecords := []string{
	"0012Exxx2QAG",
	"0012Exxx3QAG",
	"0032ExxxEjQAJ",
	"0032ExxxEkQAJ",
}

resource := collections.NewResources(session)
values, err := resource.Delete(true, deleteRecords)
if err != nil {
	fmt.Printf("Collection Error %s\n", err.Error())
	return
}

fmt.Println("Collections Deleted")
fmt.Println("-------------------")
for _, value := range values {
	fmt.Printf("%+v\n", value)
}
fmt.Println()

Retrieve Multiple Records
var queryRecords []sobject.Querier

acc1 := &query{
	sobject: "Account",
	fields: []string{
		"Name",
		"Phone",
	},
	id: "0012Exxx2QAG",
}
queryRecords = append(queryRecords, acc1)
acc2 := &query{
	sobject: "Account",
	fields: []string{
		"Name",
		"Active__c",
	},
	id: "0012Exxx3QAG",
}
queryRecords = append(queryRecords, acc2)

resource := collections.NewResources(session)
values, err := resource.Query("Account", queryRecords)
if err != nil {
	fmt.Printf("Collection Error %s\n", err.Error())
	return
}

fmt.Println("Collections Inserted")
fmt.Println("-------------------")
for _, value := range values {
	fmt.Printf("%+v\n", *value)
}
fmt.Println()

Documentation

Overview

Package collections is the implementation of the SObject Collections API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeleteValue

type DeleteValue struct {
	sobject.InsertValue
}

DeleteValue is the return value from the Salesforce API.

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

Resource is the structure for the SObject Collections API.

func NewResources

func NewResources(session session.ServiceFormatter) (*Resource, error)

NewResources forms the Salesforce SObject Collections resource structure. The session formatter is required to form the proper URLs and authorization header.

func (*Resource) Delete

func (r *Resource) Delete(allOrNone bool, records []string) ([]DeleteValue, error)

Delete will remove a group of records in the Salesforce org. The records do not need to be the same SObject.

func (*Resource) Insert

func (r *Resource) Insert(allOrNone bool, records []sobject.Inserter) ([]sobject.InsertValue, error)

Insert will create a group of records in the Salesforce org. The records do not need to be the same SObject. It is the responsibility of the caller to properly chunck the records.

func (*Resource) Query

func (r *Resource) Query(sobject string, records []sobject.Querier) ([]*sfdc.Record, error)

Query will retrieve a group of records from the Salesforce org. The records to retrieve must be the same SObject.

func (*Resource) Update

func (r *Resource) Update(allOrNone bool, records []sobject.Updater) ([]UpdateValue, error)

Update will update a group of records in the Salesforce org. The records do not need to be the same SObject. It is the responsibility of the caller to properly chunck the records.

type UpdateValue

type UpdateValue struct {
	sobject.InsertValue
}

UpdateValue is the return value from the Salesforce API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL