Skip to content

Commit f54f96d

Browse files
cristiand391paustintplumdogshetzel
authored
chore: improve docs (#17)
* chore(oauth): add client credentials example * Added section for request Added section for working with connection#request() This includes information on how to use the API as well as a number of examples There are a large number of github issues related to this use-case, so hopefully this PR will reduce the number of issues/questions that people have in the future * Fix Salesforrce typo * chore: doc HTTP headers opts * chore: add cors section * chore: improve query builder docs * chore: update request examples * Update src/partials/document/cors.html.md Co-authored-by: Steve Hetzel <shetzel@salesforce.com> --------- Co-authored-by: Austin Turner <paustint@gmail.com> Co-authored-by: Andrew Plummer <plummer574@gmail.com> Co-authored-by: Steve Hetzel <shetzel@salesforce.com>
1 parent 420013d commit f54f96d

File tree

7 files changed

+208
-2
lines changed

7 files changed

+208
-2
lines changed

src/documents/document/index.html.eco

+4
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ These docs are for jsforce v3.
4747

4848
<%- @partial('document/tooling') %>
4949

50+
<%- @partial('document/request') %>
51+
52+
<%- @partial('document/cors') %>
53+
5054
<%- @partial('document/advanced') %>

src/partials/document/connection.html.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const jsforce = require('jsforce');
7777

7878
const conn = new jsforce.Connection({
7979
instanceUrl : '<your Salesforce server URL (e.g. https://na1.salesforce.com) is here>',
80-
accessToken : '<your Salesforrce OAuth2 access token is here>'
80+
accessToken : '<your Salesforce OAuth2 access token is here>'
8181
});
8282
```
8383

@@ -97,7 +97,7 @@ const conn = new jsforce.Connection({
9797
redirectUri : '<your Salesforce OAuth2 redirect URI is here>'
9898
},
9999
instanceUrl : '<your Salesforce server URL (e.g. https://na1.salesforce.com) is here>',
100-
accessToken : '<your Salesforrce OAuth2 access token is here>',
100+
accessToken : '<your Salesforce OAuth2 access token is here>',
101101
refreshToken : '<your Salesforce OAuth2 refresh token is here>'
102102
});
103103
conn.on("refresh", (accessToken, res) => {

src/partials/document/cors.html.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## CORS
2+
3+
If you're getting CORS-related errors when using jsforce in the browser it might be that the API being used doesn't support CORS, see:
4+
https://help.salesforce.com/s/articleView?id=sf.extend_code_cors.htm&type=5
5+
6+
For those cases you'll need to use the `jsforce-ajax-proxy` proxy server, check the README for setup instructions:
7+
https://github.com/jsforce/jsforce-ajax-proxy/

src/partials/document/crud.html.md

+20
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ const rets = await conn.sobject('Account')
220220
console.log('processed: ' + rets.length);
221221
```
222222

223+
### HTTP headers
224+
225+
You can pass a `headers` object containing REST API headers on any CRUD operation:
226+
227+
```javascript
228+
const rets = await conn.sobject('Account')
229+
.create(
230+
accounts,
231+
{
232+
allowRecursive: true,
233+
headers: {
234+
'Sforce-Duplicate-Rule-Header': 'allowSave=true'
235+
}
236+
}
237+
);
238+
```
239+
240+
For more info about supported headers, see:
241+
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/headers.htm
242+
223243
### Update / Delete Queried Records
224244

225245
If you want to update/delete records in Salesforce that match a specific condition in bulk,

src/partials/document/oauth2.html.md

+20
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,23 @@ console.log("User ID: " + userInfo.id);
9292
console.log("Org ID: " + userInfo.organizationId);
9393
```
9494

95+
### Client Credentials Flow (OAuth 2.0 Client Credentials Flow)
96+
97+
Similar to the JWT example, just pass the client id and secret from your connected app and use the `client_credentials` grant type.
98+
99+
For more information about the setup, see: https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_client_credentials_flow.htm&type=5
100+
101+
```javascript
102+
import { Connection } from 'jsforce';
103+
104+
const conn = new jsforce.Connection({
105+
instanceUrl: '<org instance URL>',
106+
oauth2: {
107+
clientId : '<your Salesforce OAuth2 client ID is here>',
108+
clientSecret : '<your Salesforce OAuth2 client secret is here>',
109+
loginUrl
110+
},
111+
});
112+
113+
const userInfo = await conn.authorize({ grant_type: "client_credentials" })
114+
```

src/partials/document/query.html.md

+58
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,61 @@ await conn.sobject("Contact")
209209
});
210210
```
211211

212+
#### Conditionals
213+
214+
You can define multiple `AND`/`OR` conditional expressions by passing them in an array:
215+
```javascript
216+
// The following code gets translated to this soql query:
217+
// SELECT Name FROM Contact WHERE LastName LIKE 'A%' OR LastName LIKE 'B%'
218+
//
219+
const contacts = await conn.sobject("Contact")
220+
.find({
221+
$or: [{ LastName: { $like : 'A%' }}, { LastName: { $like : 'B%'} }]
222+
}, ['Name'])
223+
224+
console.log(contacts);
225+
```
226+
227+
#### Dates
228+
229+
`jsforce.Sfdate` provides some utilities to help working dates in SOQL:
230+
https://jsforce.github.io/jsforce/classes/date.SfDate.html
231+
232+
Literals like `YESTERDAY`, `TODAY`, `TOMORROW`:
233+
234+
```javascript
235+
// SELECT Name FROM Account WHERE PersonBirthDate = TODAY
236+
//
237+
const accounts = await conn.sobject("Account")
238+
.find({
239+
PersonBirthDate: jsforce.SfDate.TODAY
240+
}, ['Name']);
241+
242+
console.log(accounts);
243+
```
244+
245+
Dynamic N days/weeks/month/quarter functions:
246+
247+
```javascript
248+
// SELECT Name FROM Account WHERE PersonBirthDate = LAST_N_WEEKS:5
249+
//
250+
const accounts = await conn.sobject("Account")
251+
.find({
252+
PersonBirthDate: jsforce.SfDate.LAST_N_WEEKS(5)
253+
}, ['Name']);
254+
255+
console.log(accounts);
256+
```
257+
258+
Even parse a JS `Date` object:
259+
```javascript
260+
// SELECT Name FROM Account WHERE PersonBirthDate = 2024-06-27
261+
//
262+
const accounts = await conn.sobject("Account")
263+
.find({
264+
PersonBirthDate: jsforce.SfDate.toDateLiteral(new Date())
265+
}, ['Name']);
266+
267+
console.log(accounts);
268+
```
269+

src/partials/document/request.html.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
---
3+
4+
## Request
5+
6+
Make REST api calls to APIs that are not explicitly supported by JSForce.
7+
8+
### Setting the URL
9+
10+
The Endpoint URL can be in one of the following formats:
11+
12+
- Absolute URL: `https://na1.salesforce.com/services/data/v32.0/sobjects/Account/describe`.
13+
- Relative path from root: `/services/data/v32.0/sobjects/Account/describe`.
14+
- Relative path from version root: `/sobjects/Account/describe`.
15+
- This is only supported if you have explicitly set a default version.
16+
17+
### Making Requests
18+
19+
You can use `Connection.request()` to make api requests.
20+
21+
For GET requests, you can pass in a string URL.
22+
23+
```javascript
24+
/* @interactive */
25+
const res = await conn.request('/services/data/v47.0/ui-api/object-info');
26+
console.log(res)
27+
```
28+
29+
If you prefer to use callbacks instead of promises, pass a callback as the second parameter.
30+
31+
```javascript
32+
const res = await conn.request('/services/data/v47.0/ui-api/object-info');
33+
console.log(res)
34+
```
35+
36+
For other HTTP methods, you can pass an object to the request method. Ensure that you serialize the body of the request.
37+
38+
```javascript
39+
/* @interactive */
40+
// Bulk API 2.0 - Query
41+
const requestBody = {
42+
operation: 'query',
43+
query: 'SELECT Id, Name FROM Account LIMIT 1000',
44+
};
45+
46+
const res = await conn
47+
.request({
48+
method: 'POST',
49+
url: '/services/data/v47.0/jobs/query',
50+
body: JSON.stringify(requestBody),
51+
headers: {
52+
'content-type': 'application/json',
53+
},
54+
});
55+
console.log(res)
56+
```
57+
58+
#### Request Helper Methods
59+
60+
In addition to `Connection.request()`, JSForce provides the following helper methods that can also be used:
61+
62+
- `Connection.requestGet()`
63+
- `Connection.requestPatch()`
64+
- `Connection.requestPost()`
65+
- `Connection.requestPut()`
66+
- `Connection.requestDelete()`
67+
68+
For `requestPatch`, `requestPost` and `requestPut`, these will be serialized automatically and the `content-type` will be set to `application/json`.
69+
70+
```javascript
71+
/* @interactive */
72+
const requestBody = {
73+
operation: 'query',
74+
query: 'SELECT Id, Name FROM Account LIMIT 1000',
75+
};
76+
77+
const res = await conn.requestPost('/services/data/v47.0/jobs/query', requestBody);
78+
console.log(res);
79+
```
80+
81+
#### Request HTTP Options
82+
83+
All request methods allow setting HTTP options to be passed to the HTTP request.
84+
85+
| Name | Type | Description |
86+
| ----------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
87+
| responseType | string | overrides the content-type from the response to change how the response is parsed. Valid values are `text/xml`, `application/xml`, `application/json`, `text/csv`. If you do not want JSForce to auto-parse the response, set this to any other value, e.x. `text`. |
88+
| noContentResponse | any | Alternative response when no content returned in response (= HTTP 204) |
89+
| transport | Transport | Transport for http api - you should not need to set this option. |
90+
91+
If you would like to opt-out of parsing, you can set the `responseType` to text. This is useful if you want the raw response from Salesforce instead of having the results automatically parsed.
92+
93+
```javascript
94+
// Get raw CSV data instead of allowing JSForce to parse the CSV to JSON
95+
const res = await conn.requestGet('/services/data/v47.0/jobs/query/7502J00000LYZC4QAP/results', { responseType: 'text' });
96+
console.log(res);
97+
```

0 commit comments

Comments
 (0)