-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
256 lines (235 loc) · 6.57 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const nforce = require('nforce')
require('dotenv').config()
/**
* Initialise connection parameters for Salesforce Connected App
*/
const org = nforce.createConnection({
clientId: process.env.CONSUMER_KEY,
clientSecret: process.env.CONSUMER_SECRET,
redirectUri: 'https://localhost:3000/oauth/_callback',
environment: 'production',
mode: 'multi'
})
/**
* Authenticate to login a user in the org
* @param {string} username - e.g. john@example.com
* @param {string} password
* @param {string} securityToken - Your user's security token.
* This can be reset in the org if needed.
*/
const authenticate = (username, password, securityToken) =>
new Promise((resolve, reject) => {
// Authenticate using multi user mode
org.authenticate(
{
username: username,
password: password,
securityToken: securityToken
},
(error, response) => {
if (!error) {
resolve(response)
} else {
reject(error)
}
}
)
})
/**
* Perform a SOQL Query
* @param {string} queryString - SOQL query
* @param {object} oauth - OAuth string received from successful authentication
*/
const query = (queryString, oauth) =>
new Promise((resolve, reject) => {
org.query(
{
query: queryString,
oauth: oauth
},
(error, response) => {
if (!error) {
resolve(response.records)
} else {
reject(error)
}
}
)
})
/**
* Create a record
* @param {string} sobjectName - Name of the SObject (e.g. Account)
* @param {object} recordData - Key value pair of Field Names and Field Values
* @param {object} oauth - OAuth string received from successful authentication
*/
const createRecord = (sobjectName, recordData, oauth) => {
const sobj = nforce.createSObject(sobjectName, recordData)
return new Promise((resolve, reject) => {
org.insert(
{
sobject: sobj,
oauth: oauth
},
(error, response) => {
if (!error) {
resolve(response)
} else {
reject(error)
}
}
)
})
}
/**
* Update a record
* @param {string} sobjectName - Name of the SObject (e.g. Account)
* @param {object} recordDataWithID - Key value pair of Field Names and Field Values
* @param {object} oauth - OAuth string received from successful authentication
*/
const updateRecord = (sobjectName, recordDataWithID, oauth) => {
const sobj = nforce.createSObject(sobjectName, recordDataWithID)
return new Promise((resolve, reject) => {
org.update(
{
sobject: sobj,
oauth: oauth
},
(error, response) => {
if (!error) {
resolve(response)
} else {
reject(error)
}
}
)
})
}
/**
* Upsert a record
* @param {string} sobjectName - Name of the SObject (e.g. Account)
* @param {object} recordDataWithID - Key value pair of Field Names and Field Values
* @param {object} oauth - OAuth string received from successful authentication
*/
const upsertRecord = (sobjectName, recordDataWithID, oauth) => {
const sobj = nforce.createSObject(sobjectName)
const recordId = !!recordDataWithID.id ? recordDataWithID.id : ''
sobj.setExternalId('Id', recordId)
Object.entries(recordDataWithID).map(([key, value]) => {
sobj.set(key, value)
})
return new Promise((resolve, reject) => {
org.upsert(
{
sobject: sobj,
oauth: oauth,
requestOpts: {
method: !recordId ? 'POST' : 'PATCH'
}
},
(error, response) => {
if (!error) {
if(!recordId) {
resolve(response)
} else {
resolve(recordDataWithID)
}
} else {
reject(error)
}
}
)
})
}
/**
* Delete a record
* @param {string} sobjectName - Name of the SObject (e.g. Account)
* @param {object} recordDataWithID - Key value pair of Field Names and Field Values
* @param {object} oauth - OAuth string received from successful authentication
*/
const deleteRecord = (sobjectName, recordDataWithID, oauth) => {
const sobj = nforce.createSObject(sobjectName, recordDataWithID)
return new Promise((resolve, reject) => {
org.delete(
{
sobject: sobj,
oauth: oauth
},
(error, response) => {
if (!error) {
resolve(response)
} else {
reject(error)
}
}
)
})
}
/**
* Mock Data
*/
const sampleData = {
Name: 'Spiffy Cleaners',
Phone: '800-555-2345',
SLA__c: 'Gold'
}
const sampleDataUpdated = {
Name: 'Spiffy Cleaners Updated',
Phone: '800-555-5555',
SLA__c: 'Gold'
}
const sampleDataUpserted = {
Name: 'Spiffy Cleaners Upserted',
Phone: '800-777-7777',
SLA__c: 'Gold'
}
/**
* Run the app
*/
const run = async () => {
try {
// Perform authentication
const oauth = await authenticate(
process.env.SALESFORCE_USERNAME,
process.env.SALESFORCE_PASSWORD,
process.env.SALESFORCE_SECURITY_TOKEN
)
console.log(oauth)
// Perform a query
let records = await query('SELECT Id, Name FROM Account LIMIT 10', oauth)
console.log(records)
// Create a record
let account = await createRecord('Account', sampleData, oauth)
console.log(account)
// Perform account query
let accountRecord = await query(
`SELECT Id, Name, SLA__c FROM Account WHERE Id = '${account.id}'`,
oauth
)
console.log('Created: ', accountRecord)
// Update a record
sampleDataUpdated.id = account.id // Assign the account ID to be updated
let accountUpdated = await updateRecord('Account', sampleDataUpdated, oauth)
console.log(accountUpdated)
// Perform another account query
let accountRecordUpdated = await query(
`SELECT Id, Name, SLA__c FROM Account WHERE Id = '${account.id}'`,
oauth
)
console.log('Updated: ', accountRecordUpdated)
// Perform account delete
let accountDeleted = await deleteRecord('Account', sampleDataUpdated, oauth)
console.log('Deleted: ', sampleDataUpdated.id)
// Upsert a record (Insert)
let accountUpserted = await upsertRecord('Account', sampleDataUpserted, oauth)
console.log('Upsert Insert:', accountUpserted)
// Prepare upsert
sampleDataUpserted.id = accountUpserted.id
sampleDataUpserted.Name = 'Spiffy Cleaners Upserted Twice'
// Upsert a record (Update)
let accountUpserted2 = await upsertRecord('Account', sampleDataUpserted, oauth)
console.log('Upsert Update:', accountUpserted2)
} catch (error) {
console.error(error)
}
}
run()