Forcex

Build StatusHex.pm VersionCoverage Status

Elixir library for interacting with the Force.com REST API.

Usage

Add Forcex to your dependency list

  defp deps do
    [ {:forcex, "~> 0.4"}
    ]
  end

At compile time Forcex will query the Force.com REST API and generate modules for all the SObjects you have configured and permission to see.

If you see a warning like

23:37:02.057 [warn]  Cannot log into SFDC API. Please ensure you have Forcex properly configured.
Got error code 400 and message %{"error" => "invalid_client_id", "error_description" => "client identifier invalid"}

You will need to configure Forcex, as noted below, and then explicitly recompile Forcex

$ mix deps.clean forcex
$ mix deps.compile forcex

You can have Forcex generate modules at compile time using the accompanying Mix task.

$ mix compile.forcex

This can also be invoked automatically by adding Forcex to your project’s compilers in mix.exs

compilers: [:forcex] ++ Mix.compilers,

Configuration

The Forcex.Client is configured to read login information either from application configuration:


config :forcex, Forcex.Client,
  username: "user@example.com",
  password: "my_super_secret_password",
  security_token: "EMAILED_FROM_SALESFORCE",
  client_id: "CONNECTED_APP_OAUTH_CLIENT_ID",
  client_secret: "CONNECTED_APP_OAUTH_CLIENT_SECRET"

or these environment variables:

HTTPoison request-specific options may also be configured:

config :forcex, :request_options,
  timeout: 20000,
  recv_timeout: :infinity

For steps on how to create a Connected App with OAuth keys and secrets, please see the Force.com REST API section on Connected Apps.

Currently, to be fully functional, the Forcex.Client must both login and locate_services.

Pagination of results is entirely manual at the moment.

client = Forcex.Client.login |> Forcex.Client.locate_services

first_page = Forcex.query("select Id, Name from Account order by CreatedDate desc", client)

second_page = first_page |> Map.get("nextRecordsUrl") |> Forcex.get(client)

Further Configuration

Forcex allows additional configuration of API endpoint and API version via the %Forcex.Client{} struct. You may also use this mechanism if you have a grant_type other than password.

This example shows how to use both an older API version and the SalesForce sandbox API.

Forcex.Client.default_config
|> Forcex.Client.login(%Forcex.Client{endpoint: "https://test.salesforce.com", api_version: "34.0"})

Current State

See https://www.salesforce.com/us/developer/docs/api_rest/

License

MIT License, see LICENSE