Getting Started
This tutorial will walk you through setting up a simple node project, generating code and making requests against the API.
Prerequisites
Before starting this guide you will need the following:
npm&nodeinstalled on your environmentthe sfdx-cli installed and a developer or scratch ORG connected
This tutorial will create records, so do not connect with a production org!
Project Setup
Create a new typescript project:
$ mkdir ts-force-example
$ cd ts-force-example
$ mkdir src
$ npm init
$ npm install -D typescript ts-node
$ npx tsc --initOpen tsconfig.json and add the following:
"experimentalDecorators": trueInstall ts-force
Configure code generation
Next we need to create the configuration file which will tell the code generator what to do:
You should now see a ts-force-config.json in the folder where you ran the command. It's contents should look something like this:
For now, just update the auth.username property to a connected sfdx-cli username.
Run Code Generation
After this completes, you should see that generated classes have been added to src/generated for the sObjects listed in our config.
Using ts-force
We will again use the sfdx-cli obtain a Salesforce access token. To do so from node, we'll need an additional dependency:
Create a new file src/index.ts and add the following code
This code does the following:
Creates a
Restinstance from via asfdx-cliorg connectionInserts an
AccountInserts 2
Contactsrelated to theAccountQueries
ContactswhereAccount.Industry = 'manufacturing'Cleans up (deletes) the created records
See it in action
Use the following command to run the code:
Next Steps
Check out the following resources:
Last updated
Was this helpful?