Back to APIs

Salesforce

salesforce.com

Automate CRM operations and data synchronization.

Using the Salesforce API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Salesforce background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Use our HTTP endpoint to subscribe to webhooks

Example code using Salesforce

Below are some working code examples of how you can use Salesforce with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import z from "zod";
3
import jsforce from "jsforce";
4
5
// Create a Salesforce account: https://developer.salesforce.com/signup
6
// jsforce SDK: https://developer.salesforce.com/docs/platform/functions/guide/develop.html#use-salesforce-apis
7
// Salesforce only provides API access for the following editions: Enterprise, Unlimited, Developer or Performance Editions
8
// Salesforce connection instance
9
const conn = new jsforce.Connection({
10
loginUrl: process.env.SF_LOGIN_URL,
11
});
12
13
// Salesforce login
14
conn.login(
15
process.env.SF_USERNAME!,
16
process.env.SF_PASSWORD! + process.env.SF_TOKEN!, // Get your token from https://help.salesforce.com/articleView?id=user_security_token.htm&type=5
17
(error, userInfo) => {
18
if (error) {
19
return console.error(error);
20
} else {
21
console.log("Salesforce login successful. User ID: " + userInfo.id);
22
}
23
}
24
);
25
26
client.defineJob({
27
id: "salesforce-create-contact",
28
name: "Salesforce create contact",
29
version: "1.0.0",
30
trigger: eventTrigger({
31
name: "salesforce-create-contact",
32
schema: z.object({
33
Name: z.string(),
34
Website: z.string(),
35
}),
36
}),
37
run: async (payload, io, ctx) => {
38
const { Name, Website } = payload;
39
40
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
41
await io.runTask(
42
"Salesforce create contact",
43
async () => {
44
// Create a new record in Salesforce
45
await conn.sobject("Account").create({ Name, Website }, (err, ret) => {
46
if (err || !ret.success) {
47
return console.error(err, ret);
48
}
49
console.log("Created record id : " + ret.id);
50
});
51
},
52
53
// Add metadata to improve how the task displays in the logs
54
{ name: "Salesforce create contact", icon: "salesforce" }
55
);
56
},
57
});