# README

SObject APIs

back

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

  • Metadata
  • Describe
  • DML
    • Insert
    • Update
    • Upsert
    • Delete
  • Query
    • With Salesforce ID
    • With external ID
  • List of Deleted records
  • List of Updated records
  • Get Attachment body
  • Get Document body

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.

Metadata

sobjResources := sobject.NewResources(session)

metadata, err := sobjResources.Metadata("Account")

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

fmt.Println("Account Metadata")
fmt.Println("-------------------")
fmt.Printf("%+v\n", metadata)

Describe

sobjResources := sobject.NewResources(session)

describe, err := sobjResources.Describe("Account")

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

fmt.Println("Account Describe")
fmt.Println("-------------------")
fmt.Printf("%+v\n", describe)

DML Insert

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

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


sobjResources := sobject.NewResources(session)

dml := &dml{
	sobject: "Account",
	fields: map[string]interface{}{
		"Name":            "Test Account",
		"MyUID__c":        "AccExID222",
		"MyCustomText__c": "My fun text",
		"Phone":           "9045551212",
	},
}

insertValue, err := sobjResources.Insert(dml)

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

fmt.Println("Account")
fmt.Println("-------------------")
fmt.Printf("%+v\n", insertValue)

DML Update

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
}


sobjResources := sobject.NewResources(session)

dml := &dml{
	sobject: "Account",
}

dml.id = "Account Salesforce ID"
dml.fields["Phone"] = "6065551212"
dml.fields["MyCustomText__c"] = "updated text"

err = sobjResources.Update(dml)

if err != nil {
	fmt.Printf("Update Error %s\n", err.Error())
	fmt.Println()
	return
}

fmt.Println("Account Updated")
fmt.Println("-------------------")

DML Upsert

type dml struct {
	sobject       string
	fields        map[string]interface{}
	id            string
	externalField 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
}
func (d *dml) ExternalField() string {
	return d.externalField
}

sobjResources := sobject.NewResources(session)

dml := &dml{
	sobject: "Account",
}
dml.id = "AccExID345"
dml.externalField = "MyUID__c"
dml.fields["Name"] = "Upsert Update"

upsertValue, err := sobjResources.Upsert(dml)

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

fmt.Println("Account Upsert")
fmt.Println("-------------------")
fmt.Printf("%+v\n", upsertValue)

DML Delete

type dml struct {
	sobject       string
	id            string
}

func (d *dml) SObject() string {
	return d.sobject
}
func (d *dml) ID() string {
	return d.id
}

sobjResources := sobject.NewResources(session)

dml := &dml{
	sobject: "Account",
	id:      "0012E00001oHQDNQA4",
}

err = sobjResources.Delete(dml)

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

fmt.Println("Account Deleted")

Query: With Salesforce ID

Return all SObject fields.

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
}

sobjResources := sobject.NewResources(session)

query := &query{
	sobject: "Account",
	id:      "Account Salesforce ID",
}

record, err := sobjResources.Query(query)
if err != nil {
	fmt.Printf("Query Error %s\n", err.Error())
	return
}

fmt.Println("Account Query")
fmt.Println("-------------------")
fmt.Printf("%+v\n", record)

Return specific SObject fields.

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
}

sobjResources := sobject.NewResources(session)

query := &query{
	sobject: "Account",
	id:      "Account Salesforce ID",
	fields: []string{
		"Name",
		"MyUID__c",
		"Phone",
		"MyCustomText__c",
	},
}

record, err := sobjResources.Query(query)
if err != nil {
	fmt.Printf("Query Error %s\n", err.Error())
	return
}

fmt.Println("Account Query")
fmt.Println("-------------------")
fmt.Printf("%+\nv", record)

Query: With External ID

Return all SObject fields.

type query struct {
	sobject  string
	id       string
	external 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
}
func (q *query) ExternalField() string {
	return q.external
}

sobjResources := sobject.NewResources(session)

query := &query{
	sobject:  "Account",
	id:       "AccExID234",
	external: "MyUID__c",
}

record, err := sobjResources.ExternalQuery(query)
if err != nil {
	fmt.Printf("Query Error %s\n", err.Error())
	return
}

fmt.Println("Account Query")
fmt.Println("-------------------")
fmt.Printf("%+v\n", record)

Return specific SObject fields.

type query struct {
	sobject  string
	id       string
	external 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
}
func (q *query) ExternalField() string {
	return q.external
}

sobjResources := sobject.NewResources(session)

query := &query{
	sobject:  "Account",
	id:       "AccExID234",
	external: "MyUID__c",
	fields: []string{
		"Name",
		"Phone",
		"MyCustomText__c",
	},
}

record, err := sobjResources.ExternalQuery(query)
if err != nil {
	fmt.Printf("Query Error %s\n", err.Error())
	return
}

fmt.Println("Account Query")
fmt.Println("-------------------")
fmt.Printf("%+v\n", record)

List of Deleted Records

sobjResources := sobject.NewResources(session)

deletedRecords, err := sobjResources.DeletedRecords("Account", time.Now().Add(time.Hour*-12), time.Now())
if err != nil {
	fmt.Printf("Deleted Records Error %s\n", err.Error())
	return
}

fmt.Println("Deleted Account Records")
fmt.Println("-------------------")
fmt.Printf("%+v\n", deletedRecords)

List of Updated Records

sobjResources := sobject.NewResources(session)

updatedRecords, err := sobjResources.UpdatedRecords("Account", time.Now().Add(time.Hour*-12), time.Now())
if err != nil {
	fmt.Printf("Deleted Records Error %s\n", err.Error())
	return
}

fmt.Println("Updated Account Records")
fmt.Println("-------------------")
fmt.Printf("%+v\n", updatedRecords)

Get Attachment and Document Content

sobjResources := sobject.NewResources(session)

attachment, err := sobjResources.GetContent("Attachment ID", sobject.AttachmentType)
if err != nil {
	fmt.Printf("Error %s\n", err.Error())
	return
}

document, err := sobjResources.GetContent("Document ID", sobject.DocumentType)
if err != nil {
	fmt.Printf("Error %s\n", err.Error())
	return
}

# Packages

Package collections is the implementation of the SObject Collections API.
No description provided by the author

# Functions

NewResources forms the Salesforce SObject resource structure.

# Constants

AttachmentType is the content blob from the Salesforce Attachment record.
DocumentType is the content blob from the Salesforce Document record.

# Structs

ActionOverride describes the objects overrides.
ChildRelationship describes the child relationship of the SObject.
DeletedRecords is the return structure listing the deleted records.
DescribeValue is a structure that is returned from the from the Salesforce API SObject describe.
Field describes the SOBject's fields.
InsertValue is the value that is returned when a record is inserted into Salesforce.
MetadataValue is the response from the SObject metadata API.
ObjectDescribe is the SObject metadata describe.
ObjectURLs is the URL for the SObject metadata.
PickListValue describes the SObject's field picklist values.
RecordTypeInfo describes the SObjects record types assocaited with it.
RecordTypeURL contains the record type's URLs.
Resources is the structure for the Salesforce APIs for SObjects.
SupportedScope describes the supported scope.
UpdatedRecords is the return structure listing the updated records.
UpsertValue is the value that is return when a record as been upserted into Salesforce.

# Interfaces

Deleter provides the parameters needed to delete a record.
ExternalQuerier is the interface used to query a SObject from Salesforce using an external ID.
Inserter provides the parameters needed insert a record.
Querier is the interface used to query a SObject from Salesforce.
Updater provides the parameters needed to update a record.
Upserter provides the parameters needed to upsert a record.

# Type aliases

ContentType is indicator of the content type in Salesforce blob.