simpleforce icon indicating copy to clipboard operation
simpleforce copied to clipboard

It sounds very promising - can not find a way to login

Open heiner-ulrich opened this issue 2 years ago • 1 comments

It should just run, should it not? Just some stupid mistake I am making? I am beginning automating things around salesforce, and golang would be my preferred environment. One can script easily with sfdx cli, but not automate with launchd et al, and I did a nice project with go some time ago.

I filled in valid credentials.

go run sf_query_simpleforce.go

All I am getting from createClient() is the following:

&{ { } simpleforce_clientid 54.0 https://xxxconsulting--xxxx.sandbox.lightning.force.com false 0x1400009acc0}

Anybody reading this can give me some help?

Cheers,

Heiner

heiner-ulrich avatar Jan 31 '23 20:01 heiner-ulrich

The createClient() example function returns the client struct of type *simpleforce.Client only.

There are methods defined with *simpleforce.Client as the receiver so when you call something like client.Query("SELECT Id, Name FROM Organization") it uses the information in the struct (like the session information and instanceURL to make the actual API call to your Saleforce instance).

Here is how to get a full basic example working, based on the quick start in the README.

Create setup.go:

package main

import "github.com/simpleforce/simpleforce"

var (
	sfURL      = "Custom or instance URL, for example, 'https://na01.salesforce.com/'"
	sfUser     = "Username of the Salesforce account."
	sfPassword = "Password of the Salesforce account."
	sfToken    = "Security token, could be omitted if Trusted IP is configured."
)

func createClient() *simpleforce.Client {
	client := simpleforce.NewClient(sfURL, simpleforce.DefaultClientID, simpleforce.DefaultAPIVersion)
	if client == nil {
		// handle the error

		return nil
	}

	err := client.LoginPassword(sfUser, sfPassword, sfToken)
	if err != nil {
		// handle the error

		return nil
	}

	// Do some other stuff with the client instance if needed.

	return client
}

Create query.go

package main

import (
    "fmt"
    "github.com/simpleforce/simpleforce"
)

func Query() {
	client := createClient()

	q := "SELECT Id, Name FROM Organization"
	result, err := client.Query(q) // Note: for Tooling API, use client.Tooling().Query(q)
	if err != nil {
		// handle the error
		return
	}

	for _, record := range result.Records {
		// access the record as SObjects.
		fmt.Println(record)
	}
}

Create main.go:

package main

func main() {
	Query()
}

Finally, run the example:

go mod tidy
go run .

alain-odea-sequence avatar Mar 13 '23 17:03 alain-odea-sequence