Qore SalesforceRestClient Module Reference  1.1
 All Classes Namespaces Functions Variables Groups Pages

SalesforceRestClient Introduction

The SalesforceRestClient module provides an API for calling REST services with Salesforce.com.

To use this module, use "%requires SalesforceRestClient" in your code.

All the public symbols in the module are defined in the SalesforceRestClient namespace.

The main classes are:

  • SalesforceRestClient: this class provides the REST client API for communuication with Salesforce.com; it also automates authentication and authorization to the target Connected App
  • SalesforceRestConnection: provides a REST connection object to a Salesforce.com server (based on the ConnectionProvider module)
Example:
1 #!/usr/bin/env qore
2 
3 %new-style
4 %strict-args
5 %require-types
6 %enable-all-warnings
7 
8 %requires SalesforceRestClient
9 
10 hash opts = (
11  "client_id": ENV.SALESFORCE_CONSUMER_KEY,
12  "client_secret": ENV.SALESFORCE_CONSUMER_SECRET,
13  "username": ENV.SALESFORCE_USER,
14  "password": ENV.SALESFORCE_PASS,
15 );
16 
17 SalesforceRestClient rest(opts);
18 hash ans = rest.get("sobjects");
19 printf("%N\n", ans.body);

The composite API can be used to work with multiple objects at once as in the following example:

Example:
1 list sub create_accounts(list account_list) {
2  # get list of account numbers for return value
3  list account_numbers = map $1.AccountNumber, account_list;
4 
5  # add reference IDs for composite REST call
6  account_list = map $1 + ("attributes": ("type": "Account", "referenceId": $#.toString())), account_list;
7 
8  # create accounts
9  hash info;
10  on_error printf("ERROR info: %N", info);
11  # maximum 200 accounts can be created; if any errors occur, an exception is thrown
12  rc.post("composite/tree/Account", ("records": account_list), \info);
13 
14  log(LL_INFO, "created accounts: %y", account_numbers);
15  return account_numbers;
16 }

Requests can also be made with the Salesforce.com Bulk REST API; the following example shows how a list of accounts can be deleted:

Example:
1 #!/usr/bin/env qore
2 
3 %new-style
4 %strict-args
5 %require-types
6 %enable-all-warnings
7 
8 %requires SalesforceRestClient
9 %requires json
10 
11 hash opts = (
12  "client_id": ENV.SALESFORCE_CONSUMER_KEY,
13  "client_secret": ENV.SALESFORCE_CONSUMER_SECRET,
14  "username": ENV.SALESFORCE_USER,
15  "password": ENV.SALESFORCE_PASS,
16 );
17 
18 SalesforceRestClient rest(opts);
19 
20 # get list of Account IDs to delete with the REST API with an SOQL query
21 list al = map ("Id": $1.Id), rc.get("query?q=select Id, Name from Account where Name like 'Account %%'").body.records;
22 # create job with the Bulk REST API
23 hash h = rc.bulkJobCreate(BulkJobDelete, "Account", BulkJobJson).jobInfo;
24 printf("created job %y\n", h.id);
25 # create JSON data
26 string data = make_json(al);
27 # add batch to job with the Bulk REST API
28 rc.bulkJobAddBatch(h.id, data, BulkJobJson, \info);
29 # close job with the Bulk REST API
30 rc.bulkJobClose(h.id);
See Also
"sfrest" in the bin directory for a user-friendly command-line interface to Salesforce.com REST API functionality and a more detailed example of code using this module.

Release Notes

SalesforceRestClient v1.1

SalesforceRestClient v1.0

  • the initial version of the SalesforceRestClient module