-
Notifications
You must be signed in to change notification settings - Fork 7
/
EXAMPLES
451 lines (311 loc) · 11.2 KB
/
EXAMPLES
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
All of the examples work against either the Enterprise WSDL or the Partner WSDL.
The examples will be in the same order as they are found in the Salesforce API Reference at
http://www.salesforce.com/us/developer/docs/api/index.htm
NOTE: These examples are not intended to teach the Salesforce API; rather, they are here to
illustrate how the Python Toolkit implements the Salesforce API.
Prerequisites
-------------
All examples below assume the Toolkit has been instantiated and the connection has been made.
Partner WSDL
------------
from sforce.partner import SforcePartnerClient
h = SforcePartnerClient('/path/to/partner.wsdl.xml')
Enterprise WSDL
---------------
from sforce.enterprise import SforceEnterpriseClient
h = SforceEnterpriseClient('/path/to/enterprise.wsdl.xml')
Additionally, all examples except login() assume you are logged in, using a call such as
h.login('[email protected]', '*passwordhere*', '*securitytokenhere*')
Conventions
-----------
<create lead> - implies that you copy and paste the code from the 'create lead' section of create(),
so you have a Lead in the variable 'lead' and a SaveResult in the variable 'result'
<create 2 leads> - same as create lead, except copy and past the code from 'create 2 leads' in
create()
Variable Names:
---------------
h - reference to an instance of the Toolkit object
result - the result of a Salesforce method call
lead - a lead object
Return Typing
-------------
All of the examples assume loose return typing, where a create() call that creates a single object,
for instance, returns a single SaveResult, and a create() call that creates multiple objects returns
a list of SaveResult objects.
----------------------------------------------------------------------------------------------------
Core Calls:
convertLead()
-------------
<create lead>
leadConvert = h.generateObject('LeadConvert')
leadConvert.leadId = result.id
# the possible values for convertedStatus depend on what's in the picklist for your org
# you can take a look at the 'Convert Lead' screen in the UI for your API user to see what's there
leadConvert.convertedStatus = 'Qualified'
result = h.convertLead(leadConvert)
create()
--------
# create lead
lead = h.generateObject('Lead')
lead.FirstName = 'Joe'
lead.LastName = 'Moke'
lead.Company = 'Jamoke, Inc.'
lead.Email = '[email protected]'
result = h.create(lead)
or
# create 2 leads
lead = h.generateObject('Lead')
lead.FirstName = 'Joe'
lead.LastName = 'Moke'
lead.Company = 'Jamoke, Inc.'
lead.Email = '[email protected]'
lead2 = h.generateObject('Lead')
lead2.FirstName = 'Bob'
lead2.LastName = 'Moke'
lead2.Company = 'Jamoke, Inc.'
lead2.Email = '[email protected]'
result = h.create((lead, lead2))
delete()
--------
<create lead>
result = h.delete(result.id)
or
<create 2 leads>
ids = []
for SaveResult in result:
ids.append(SaveResult.id)
result = h.delete(ids)
emptyRecycleBin()
-----------------
result = h.emptyRecycleBin('*ID HERE*')
or
result = h.emptyRecycleBin(('*ID HERE*', '*ANOTHER ID HERE*'))
getDeleted()
------------
result = h.getDeleted('Lead', '2009-06-01T23:01:01Z', '2019-01-01T23:01:01Z')
getUpdated()
------------
result = h.getUpdated('Lead', '2009-06-01T23:01:01Z', '2019-01-01T23:01:01Z')
invalidateSessions()
--------------------
result = h.invalidateSessions(h.getSessionId())
or
result = h.invalidateSessions((h.getSessionId(), '*ANOTHER SESSION ID HERE*'))
login()
-------
h.login('[email protected]', '*passwordhere*', '*securitytokenhere*')
logout()
--------
result = h.logout()
merge()
-------
<create two leads with variable names lead and lead2>
# set the corresponding resulting id in 'lead'
lead.Id = result[0].id
mergeRequest = h.generateObject('MergeRequest')
mergeRequest.masterRecord = lead
mergeRequest.recordToMergeIds = result[1].id
result = h.merge(mergeRequest)
process()
---------
NOTE: The documentation for this call is currently incorrect, the Process*Request objects take a
property 'comments', not 'comment' as stated here:
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_process.htm
However, judging from the fact that it's still incorrect in the docs, it's a similar issue
to protected inheritance in C++ - it's in there 'for completeness' :-)
By the way, doesn't that just sum up half of C++?
processRequest = h.generateObject('ProcessSubmitRequest')
processRequest.objectId = '*ID OF OBJECT PROCESS REQUEST AFFECTS*'
processRequest.comments = 'This is what I think.'
result = h.process(processRequest)
or
processRequest = h.generateObject('ProcessWorkitemRequest')
processRequest.action = 'Approve'
processRequest.workitemId = '*ID OF OBJECT PROCESS REQUEST AFFECTS*'
processRequest.comments = 'I approved this request.'
result = h.process(processRequest)
query()
-------
result = h.query('SELECT FirstName, LastName FROM Lead')
queryAll()
----------
result = h.queryAll('SELECT Account.Name, FirstName, LastName FROM Contact LIMIT 2')
for record in result.records:
print record.FirstName, record.LastName
print record.Account.Name
queryMore()
-----------
queryOptions = h.generateHeader('QueryOptions')
queryOptions.batchSize = 200
h.setQueryOptions(queryOptions)
result = h.queryAll('SELECT FirstName, LastName FROM Lead')
result = h.queryMore(result.queryLocator)
# result.done = True indicates the final batch of results has been sent
retrieve()
----------
result = h.retrieve('FirstName, LastName, Company, Email', 'Lead', '00QR0000002yyVs')
or
result = h.retrieve('FirstName, LastName, Company, Email', 'Lead', ('00QR0000002yyVs', '00QR0000003HWMPLA4'))
search()
--------
result = h.search('FIND {Joe Moke} IN Name Fields RETURNING Lead(Name, Phone)')
undelete()
----------
result = h.undelete('*ID HERE*')
or
result = h.undelete(('*ID HERE*', '*ANOTHER ID HERE*'))
update()
--------
<create lead>
lead.Id = result.id
# As a single value, 'Email' would not _have_ to be wrapped in a tuple/list
lead.fieldsToNull = ('Email')
# to set a value to NULL in Salesforce, must also remove the value from the lead variable or it will
# give you a 'Duplicate Values...' exception message
lead.Email = None
h.update(lead)
upsert()
--------
<create lead>
lead.Id = result.id
lead.FirstName = 'Bob'
h.upsert('Id', lead)
----------------------------------------------------------------------------------------------------
Describe Calls:
describeGlobal()
----------------
result = h.describeGlobal()
describeLayout()
----------------
result = h.describeLayout('Lead', '012000000000000AAA') # Master Record Type
describeSObject()
-----------------
result = h.describeSObject('Lead')
describeSObjects()
------------------
result = h.describeSObjects(('Lead', 'Contact'))
describeTabs()
--------------
result = h.describeTabs()
----------------------------------------------------------------------------------------------------
Utility Calls:
getServerTimestamp()
--------------------
result = h.getServerTimestamp()
getUserInfo()
-------------
result = h.getUserInfo()
resetPassword()
---------------
result = h.resetPassword('*USER ID HERE*')
sendEmail()
-----------
# Email a single person
email = h.generateObject('SingleEmailMessage')
email.toAddresses = '[email protected]'
email.subject = 'This is my subject.'
email.plainTextBody = 'This is the plain-text body of my email.'
result = h.sendEmail([email])
# MassEmailMessage
email = h.generateObject('MassEmailMessage')
email.targetObjectIds = (('*LEAD OR CONTACT ID TO EMAIL*', '*ANOTHER LEAD OR CONTACT TO EMAIL*'))
email.templateId = '*EMAIL TEMPLATE ID TO USE*'
result = h.sendEmail([email])
setPassword()
-------------
result = h.setPassword('*USER ID HERE*', '*NEW PASSWORD HERE*')
----------------------------------------------------------------------------------------------------
Toolkit-Specific Utility Calls:
generateHeader()
----------------
header = h.generateHeader('AllowFieldTruncationHeader');
generateObject()
----------------
lead = h.generateObject('Lead')
getLastRequest()
----------------
result = h.getLastRequest()
getLastResponse()
-----------------
result = h.getLastResponse()
----------------------------------------------------------------------------------------------------
SOAP Headers
Note that you need only to call the appropriate set*() call once per header, and the header will
automatically be attached to the SOAP envelope for the calls that it pertains to. The SessionHeader
header is set for you after a successful login() call, but it's a public method for the edge case
where you need to piggyback on another user's session.
AllowFieldTruncationHeader
--------------------------
header = h.generateHeader('AllowFieldTruncationHeader');
header.allowFieldTruncation = False
h.setAllowFieldTruncationHeader(header)
AssignmentRuleHeader
--------------------------
header = h.generateHeader('AssignmentRuleHeader');
header.useDefaultRule = True
h.setAssignmentRuleHeader(header)
or
header = h.generateHeader('AssignmentRuleHeader');
header.assignmentRuleId = '*ASSIGNMENT RULE ID HERE*'
h.setAssignmentRuleHeader(header)
CallOptions
--------------------------
Note that this header only applies to the Partner WSDL.
header = h.generateHeader('CallOptions');
header.client = '*MY CLIENT STRING*'
header.defaultNamespace = '*DEVELOPER NAMESPACE PREFIX*'
h.setCallOptions(header)
EmailHeader
--------------------------
header = h.generateHeader('EmailHeader');
header.triggerAutoResponseEmail = True
header.triggerOtherEmail = True
header.triggerUserEmail = True
h.setEmailHeader(header)
LocaleOptions
--------------------------
header = h.generateHeader('LocaleOptions');
header.language = 'en_US'
h.setLocaleOptions(header)
LoginScopeHeader
--------------------------
header = h.generateHeader('LoginScopeHeader');
header.organizationId = '*YOUR ORGANIZATION ID HERE*'
header.portalId = '*YOUR ORGANIZATION\'S PORTAL ID HERE*'
h.setLoginScopeHeader(header)
MruHeader
--------------------------
header = h.generateHeader('MruHeader');
header.updateMru = True
h.setMruHeader(header)
PackageVersionHeader
--------------------------
NOTE: I believe this example works. Salesforce accepts the header, and the call in which the
header is embedded succeeds, but as there is no available documentation on what the XML
request should look like, and since I don't have any APEX code available that deals with
package versions, I can't be 100% sure. It is new in version 16.0 of the API, so
documentation should materialize soon :)
Note also that this is the only header that takes something other than a series of key-value
pairs.
header = h.generateHeader('PackageVersionHeader');
pkg = {}
pkg['majorNumber'] = 3
pkg['minorNumber'] = 0
pkg['namespace'] = 'SFGA'
header.packageVersions = [pkg, pkg]
h.setPackageVersionHeader(header)
QueryOptions
--------------------------
header = h.generateHeader('QueryOptions');
header.batchSize = 200
h.setQueryOptions(header)
SessionHeader
--------------------------
header = h.generateHeader('SessionHeader');
header.sessionId = '*PIGGYBACK SESSION ID HERE*'
h.setSessionHeader(header)
UserTerritoryDeleteHeader
--------------------------
header = h.generateHeader('UserTerritoryDeleteHeader');
header.transferToUserId = '*USER ID HERE*'
h.setUserTerritoryDeleteHeader(header)