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
- With
- List of Deleted records
- List of Updated records
- Get
Attachment
body - Get
Document
body
As a reference, see Salesforce API
documentation
The following are examples to access the APIs
. It is assumed that a go-sfdc
session has been created.
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)
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)
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)
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("-------------------")
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)
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")
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)
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)
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)
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)
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
}