goforce

package module
v0.0.0-...-9373277 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2021 License: MIT Imports: 10 Imported by: 0

README

goforce

Simple package for interacting with the Salesforce API that currently supports interacting with SObjects. The only piece of magic this package does is logging back in for you if you are making a JSON request and get a 401 (Salesforce sessions expire after 2 hours).

Login

import gitlab.com/wjwatkinson/goforce

session := goforce.Login("username", "password", "token", "clientID", DefautlAPIVersion, DefaultLoginURL, http.Client)

Query

q := "select id, account.name from contact where email = 'example@email.com'"
qResp, err := session.SOQuery(q)
if err != nil {
  // handle error
}

if qResp.Success == false {
  // handle unsuccessful request
  errCode := upResp.Errors[0].ErrorCode
  errMsg := upResp.Error[0].Message
}

records := qResp.Records

Update

upData := map[string]interface{}{
  "FirstName": "John",
  "LastNAme": "Doe",
}

upResp, err := session.SOUpdate("contact", "contact-id", upData)
if err != nil {
  // handle error
}

if upResp.Success == false {
  // handle unsuccessful request
}

Create

data := map[string]interface{}{
  "FirstName": "John",
  "LastName": "Doe",
  "Email": "john.doe@gmail.com",
}

crResp, err := session.SOCreate("contact", data)
if err != nil {
  // handle error
}

if crResp.Success == false {a
  // handle unsuccessful request
}

contID := crResp.ID

Upsert

data := map[string]interface{}{
  "FirstName": "John",
  "LastName": "Doe",
  "Email": "john.doe@gmail.com",
  "shopify_id__c": "12335",
}

upsResp, err := session.SOUpsert("contact", "shopify_id__c", "12335", data)
if err != nil {
  // handle error
}

if crResp.Success == false {
  // handle unsuccessful request
}

if crResp.Created == true {
  // handle any additional logic for newly created contacts
}

contID := crResp.ID

Delete

resp, err := session.SODelete("contact", "contact-id")
if err != nil {
  // handle error
}

if resp.Success == false {
  // handle unsuccessful request
}

Documentation

Index

Constants

View Source
const (
	DefaultAPIVersion = "50.0"
	DefaultClientID   = "goforce"
	DefaultLoginURL   = "https://login.salesforce.com"
)

Variables

This section is empty.

Functions

func ParseXMLError

func ParseXMLError(resp *http.Response) error

Types

type CreateResponse

type CreateResponse struct {
	ID         string
	StatusCode int
	Success    bool
	Errors     JSONErrors
}

type DescribeResponse

type DescribeResponse struct {
	Body       map[string]interface{}
	Success    bool
	StatusCode int
	Errors     JSONErrors
}

type JSONErrors

type JSONErrors []struct {
	Message   string `json:"message"`
	ErrorCode string `json:"erorrCode"`
}

type JSONResponse

type JSONResponse struct {
	Body       []byte
	Success    bool
	StatusCode int
	Errors     JSONErrors
}

type QueryResult

type QueryResult struct {
	TotalSize      int                      `json:"totalSize"`
	Done           bool                     `json:"done"`
	NextRecordsURL string                   `json:"nextRecordsUrl"`
	Records        []map[string]interface{} `json:"records"`
	Success        bool
	StatusCode     int
}

QueryResult holds the response data from an SOQL query.

type Session

type Session struct {
	InstanceURL string
	ID          string
	Username    string
	Password    string
	Token       string
	ClientID    string
	APIVersion  string
	LoginURL    string
	HTTPClient  http.Client
}

func Login

func Login(username string, password string, token string, clientID string, apiVersion string, loginURL string, httpClient http.Client) (*Session, error)

LoginPassword signs into salesforce using password. token is optional if trusted IP is configured. Ref: https://developer.salesforce.com/docs/atlas.en-us.214.0.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm Ref: https://developer.salesforce.com/docs/atlas.en-us.214.0.api.meta/api/sforce_api_calls_login.htm

func (*Session) JSONRequest

func (session *Session) JSONRequest(method, u string, data map[string]interface{}) (*JSONResponse, error)

func (*Session) MakeSObjectsURL

func (session *Session) MakeSObjectsURL() string

func (*Session) SODescribe

func (session *Session) SODescribe(sobject string) (*DescribeResponse, error)

func (*Session) SOQuery

func (session *Session) SOQuery(query string) (*QueryResult, error)

Perform SOQL Query

func (*Session) SOUpsert

func (session *Session) SOUpsert(sobject string, extIDFName string, extID string, data map[string]interface{}) (*UpsertResponse, error)

Upsert https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm

type UpsertResponse

type UpsertResponse struct {
	ID         string
	Created    bool
	StatusCode int
	Success    bool
	Errors     JSONErrors
}

Jump to

Keyboard shortcuts

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