Skip to content

Latest commit

 

History

History
415 lines (348 loc) · 7.65 KB

README.md

File metadata and controls

415 lines (348 loc) · 7.65 KB

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
}