tree

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: 9 Imported by: 0

README

SObject Tree API

back

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

  • Create Multiple Records with Children

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.

Builder
type treeBuilder struct {
	sobject     string
	fields      map[string]interface{}
	referenceID string
}

func (b *treeBuilder) SObject() string {
	return b.sobject
}
func (b *treeBuilder) Fields() map[string]interface{} {
	return b.fields
}
func (b *treeBuilder) ReferenceID() string {
	return b.referenceID
}

// build some records
accountRef1Builder := &treeBuilder{
	sobject:     "Account",
	referenceID: "ref1",
	fields: map[string]interface{}{
		"name":              "SampleAccount11",
		"phone":             "1234567890",
		"website":           "www.salesforce.com",
		"numberOfEmployees": 100,
		"industry":          "Banking",
	},
}
accountRef2Builder := &treeBuilder{
	sobject:     "Account",
	referenceID: "ref4",
	fields: map[string]interface{}{
		"name":              "SampleAccount112",
		"Phone":             "1234567890",
		"website":           "www.salesforce2.com",
		"numberOfEmployees": 100,
		"industry":          "Banking",
	},
}
contactRef3Builder := &treeBuilder{
	sobject:     "Contact",
	referenceID: "ref2",
	fields: map[string]interface{}{
		"lastname": "Smith11",
		"title":    "President",
		"email":    "sample@salesforce.com",
	},
}
contactRef4Builder := &treeBuilder{
	sobject:     "Contact",
	referenceID: "ref3",
	fields: map[string]interface{}{
		"lastname": "Evans11",
		"title":    "Vice President",
		"email":    "sample@salesforce.com",
	},
}

account1RecordBuilder, err := tree.NewRecordBuilder(accountRef1Builder)
if err != nil {
	fmt.Printf("NewRecordBuilder Error %s\n", err.Error())
	return
}
contact1RecordBuilder, err := tree.NewRecordBuilder(contactRef3Builder)
if err != nil {
	fmt.Printf("NewRecordBuilder Error %s\n", err.Error())
	return
}
contact2RecordBuilder, err := tree.NewRecordBuilder(contactRef4Builder)
if err != nil {
	fmt.Printf("NewRecordBuilder Error %s\n", err.Error())
	return
}
account1RecordBuilder.SubRecords("contacts", contact1RecordBuilder.Build(), contact2RecordBuilder.Build())

account2RecordBuilder, err := tree.NewRecordBuilder(accountRef2Builder)
if err != nil {
	fmt.Printf("NewRecordBuilder Error %s\n", err.Error())
	return
}

Create Accounts with Children
inserter := &treeInserter{
	sobject: "Account",
	records: []*tree.Record{
		account1RecordBuilder.Build(),
		account2RecordBuilder.Build(),
	},
}
resource := tree.NewResource(session)
value, err := resource.Insert(inserter)
if err != nil {
	fmt.Printf("resource.Insert Error %s\n", err.Error())
	return
}
fmt.Printf("%+v\n", *value)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attributes

type Attributes struct {
	Type        string `json:"type"`
	ReferenceID string `json:"referenceId"`
}

Attributes are the attributes of the composite tree.

type Builder

type Builder interface {
	sobject.Inserter
	ReferenceID() string
}

Builder is the SObject Tree builder for the composite SObject Tree API.

type InsertValue

type InsertValue struct {
	ReferenceID string       `json:"referenceId"`
	ID          string       `json:"id"`
	Errors      []sfdc.Error `json:"errors"`
}

InsertValue is the return value for each record.

type Inserter

type Inserter interface {
	SObject() string
	Records() []*Record
}

Inserter is used to define the SObject and it's records for the composite tree API.

type Record

type Record struct {
	Attributes Attributes
	Fields     map[string]interface{}
	Records    map[string][]*Record
}

Record is the composite tree SObject.

func (*Record) MarshalJSON

func (r *Record) MarshalJSON() ([]byte, error)

MarshalJSON will create the JSON byte array.

type RecordBuilder

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

RecordBuilder is record builder for the composite SObject Tree API.

func NewRecordBuilder

func NewRecordBuilder(builder Builder) (*RecordBuilder, error)

NewRecordBuilder will create a new builder. If the SObject is not value or the reference ID is empyt, an error will be returned.

func (*RecordBuilder) Build

func (rb *RecordBuilder) Build() *Record

Build will create the composite tree record.

func (*RecordBuilder) SubRecords

func (rb *RecordBuilder) SubRecords(sobjects string, records ...*Record)

SubRecords will add subrecords to the object.

type Resource

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

Resource is the composite tree API resource.

func NewResource

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

NewResource creates a new composite tree resource from the session.

func (*Resource) Insert

func (r *Resource) Insert(inserter Inserter) (*Value, error)

Insert will call the composite tree API.

type Value

type Value struct {
	HasErrors bool          `json:"hasErrors"`
	Results   []InsertValue `json:"results"`
}

Value is the return value from the API call.

Jump to

Keyboard shortcuts

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