-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSalesForce.cfc
More file actions
995 lines (842 loc) · 41 KB
/
SalesForce.cfc
File metadata and controls
995 lines (842 loc) · 41 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
<cfcomponent displayname="SalesForceCFC" output="false">
<cffunction name="init" access="public" output="false" returntype="SalesForce">
<cfargument name="username" type="string" required="true" />
<cfargument name="password" type="string" required="true" />
<cfargument name="loginURL" type="string" required="false" default="https://www.salesforce.com/services/Soap/u/11.1" />
<cfargument name="portalURL" type="string" required="false" default="https://na3.salesforce.com" />
<cfargument name="soapTimeout" type="numeric" required="false" default="60" />
<cfargument name="autoLogin" type="boolean" required="false" default="true" />
<cfscript>
variables.instance = structNew();
variables.instance.sessionId = '';
variables.instance.serverURL = '';
variables.instance.lastLogin = '';
setUserName(arguments.username);
setPassword(arguments.password);
setLoginURL(arguments.loginURL);
setPortalURL(arguments.portalURL);
setSOAPTimeout(arguments.soapTimeout);
setAutoLogin(arguments.autoLogin);
</cfscript>
<cfreturn this />
</cffunction>
<!--- GETTERS / SETTERS --->
<cffunction name="getMemento" access="public" returntype="struct" output="false">
<cfreturn variables.instance />
</cffunction>
<cffunction name="getVersion" access="public" returntype="string" output="false">
<cfreturn '0.8' />
</cffunction>
<cffunction name="getSessionId" access="public" returntype="string" output="false">
<cfreturn variables.instance.sessionId />
</cffunction>
<cffunction name="getServerURL" access="public" returntype="string" output="false">
<cfreturn variables.instance.serverURL />
</cffunction>
<cffunction name="getLastLogin" access="public" returntype="string" output="false">
<cfreturn variables.instance.lastLogin />
</cffunction>
<cffunction name="getUserName" access="public" returntype="string" output="false">
<cfreturn variables.instance.username />
</cffunction>
<cffunction name="setUserName" access="public" returntype="void" output="false">
<cfargument name="username" type="string" required="true" />
<cfset variables.instance.username = arguments.username />
</cffunction>
<cffunction name="getPassword" access="public" returntype="string" output="false">
<cfreturn variables.instance.password />
</cffunction>
<cffunction name="setPassword" access="public" returntype="void" output="false">
<cfargument name="password" type="string" required="true" />
<cfset variables.instance.password = arguments.password />
</cffunction>
<cffunction name="getLoginURL" access="public" returntype="string" output="false">
<cfreturn variables.instance.loginURL />
</cffunction>
<cffunction name="setLoginURL" access="public" returntype="void" output="false">
<cfargument name="loginURL" type="string" required="true" />
<cfset variables.instance.loginURL = arguments.loginURL />
</cffunction>
<cffunction name="getPortalURL" access="public" returntype="string" output="false">
<cfreturn variables.instance.portalURL />
</cffunction>
<cffunction name="setPortalURL" access="public" returntype="void" output="false">
<cfargument name="portalURL" type="string" required="true" />
<cfset variables.instance.portalURL = arguments.portalURL />
</cffunction>
<cffunction name="getSoapTimeout" access="public" returntype="numeric" output="false">
<cfreturn variables.instance.soapTimeout />
</cffunction>
<cffunction name="setSoapTimeout" access="public" returntype="void" output="false">
<cfargument name="soapTimeout" type="numeric" required="true" />
<cfset variables.instance.soapTimeout = arguments.soapTimeout />
</cffunction>
<cffunction name="getAutoLogin" access="public" returntype="boolean" output="false">
<cfreturn variables.instance.autoLogin />
</cffunction>
<cffunction name="setAutoLogin" access="public" returntype="void" output="false">
<cfargument name="autoLogin" type="boolean" required="true" />
<cfset variables.instance.autoLogin = arguments.autoLogin />
</cffunction>
<!--- SF CORE CALLS --->
<cffunction name="login" access="public" output="false" returntype="struct">
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cftry>
<cfif not structKeyExists(variables,'instance')>
<cfthrow message="Object not initialized" detail="Create an instance of object with init() method" type="salesForceCFC" />
<cfelseif not len(getUserName())>
<cfthrow message="Username is required." detail="Username is empty. Use setUserName() method." type="salesForceCFC" />
<cfelseif not len(getPassword())>
<cfthrow message="Password is required." detail="Password is empty. Use setPassword() method." type="salesForceCFC" />
</cfif>
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com">
<soapenv:Body>
<urn:login>
<urn:username>#getUserName()#</urn:username>
<urn:password>#getPassword()#</urn:password>
</urn:login>
</soapenv:Body>
</soapenv:Envelope>
</cfoutput>
</cfsavecontent>
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(requestURL = getLoginURL(), requestSOAP = stLocal.requestSOAP, generateHeader = false) />
<!--- parse out session values --->
<cfset stLocal.sessionId = xmlSearch(stLocal.soapXML, "//*[name()='sessionId']") />
<cfset stLocal.serverUrl = xmlSearch(stLocal.soapXML, "//*[name()='serverUrl']") />
<!--- if valid response set values --->
<cfif arraylen(stLocal.sessionId) and arrayLen(stLocal.serverUrl)>
<cfset variables.instance.serverURL = stLocal.serverUrl[1].xmlText />
<cfset variables.instance.sessionId = stLocal.sessionId[1].xmlText />
<cfset variables.instance.lastLogin = now() />
<!--- on unknown return response --->
<cfelse>
<cfthrow message="Unknown response" detail="#trim(stLocal.soapRequest.results.fileContent)#" />
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="retrieveObject" access="public" output="false" returntype="struct">
<cfargument name="objectType" type="string" required="true" />
<cfargument name="fieldList" type="string" required="true" />
<cfargument name="idList" type="string" required="true" />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = arrayNew(1) />
<cftry>
<cfset validate() />
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:retrieve>
<urn:fieldList>#arguments.fieldList#</urn:fieldList>
<urn:sObjectType>#arguments.objectType#</urn:sObjectType>
<cfloop list="#arguments.idList#" index="stLocal.iId">
<urn:ids>#stLocal.iId#</urn:ids>
</cfloop>
</urn:retrieve>
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(requestURL = getServerURL(), requestSOAP = stLocal.requestSOAP) />
<!--- parse out result --->
<cfset stLocal.result = xmlSearch(stLocal.soapXML, "//*[name()='result']") />
<!--- if valid response set values --->
<cfif arraylen(stLocal.result)>
<!--- create list of columns for query --->
<cfset stLocal.fieldList = structNew() />
<!--- loop thru records to build query --->
<cfloop from="1" to="#arrayLen(stLocal.result[1].xmlChildren)#" index="stLocal.iColumn">
<cfset stLocal.fieldList[listLast(stLocal.result[1].xmlChildren[stLocal.iColumn].xmlName,':')] = '' />
</cfloop>
<!--- loop thru field children to build info into struct --->
<cfloop from="1" to="#arrayLen(stLocal.result)#" index="stLocal.iRecord">
<!--- loop thru records to build query --->
<cfloop from="1" to="#arrayLen(stLocal.result[stLocal.iRecord].xmlChildren)#" index="stLocal.iColValue">
<cfset stLocal.fieldList[listLast(stLocal.result[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlName,':')] = stLocal.result[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlText />
</cfloop>
<cfset arrayAppend(stReturn.results,stLocal.fieldList) />
</cfloop>
<!--- on unknown return response --->
<cfelse>
<cfthrow message="Unknown response" detail="#trim(stLocal.soapRequest.results.fileContent)#" />
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="queryObject" access="public" output="false" returntype="struct">
<cfargument name="queryString" type="string" required="true" />
<cfargument name="includeRelatedObject" type="boolean" required="false" default="true" />
<cfargument name="batchSize" type="numeric" required="false" default="0" hint="Minimum of 200. Maximum of 2,000." />
<cfargument name="disablePagination" type="boolean" required="false" default="false" hint="Will use queryMore to fetch all records." />
<cfset var stSOAPArgs = structNew() />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = '' />
<cftry>
<cfset validate() />
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:query>
<urn:queryString>#xmlFormat(arguments.queryString)#</urn:queryString>
</urn:query>
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<cfif not arguments.disablePagination and arguments.batchSize gt 0>
<cfset stSOAPArgs.appendToHeader = buildQueryOptions(batchsize = arguments.batchSize) />
</cfif>
<cfset stSOAPArgs.requestURL = getServerURL() />
<cfset stSOAPArgs.requestSOAP = stLocal.requestSOAP />
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(argumentCollection = stSOAPArgs) />
<!--- check for valid response --->
<cfset stLocal.result = xmlSearch(stLocal.soapXML, "//*[name()='result']") />
<!--- return soap response when unknown --->
<cfif not arrayLen(stLocal.result)>
<cfthrow message="Unknown response" detail="#trim(arguments.soapRequest.results.fileContent)#" />
</cfif>
<!--- parse query results --->
<cfset stLocal.queryResults = parseQueryXML(stLocal.soapXML, arguments.includeRelatedObject, arguments.batchSize) />
<cfif not stLocal.queryResults.success>
<cfthrow object="#stLocal.queryResults.error#" />
</cfif>
<cfset stReturn = stLocal.queryResults />
<cfif arguments.disablePagination and structKeyExists(stReturn,'queryLocator') and isQuery(stReturn.results)>
<cfloop condition="stReturn.results.recordCount lt stReturn.size">
<cfset stLocal.queryMore = queryMore(queryLocator = stReturn.queryLocator, startRow = stReturn.batchSize + 1, batchsize = arguments.batchSize) />
<cfif stLocal.queryMore.success>
<cfquery name="stReturn.results" dbtype="query">
SELECT *
FROM stReturn.results
UNION
SELECT *
FROM stLocal.queryMore.results
</cfquery>
<cfelse>
<cfthrow object="#stLocal.queryMore.error#" />
</cfif>
</cfloop>
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="queryMore" access="public" output="false" returntype="struct">
<cfargument name="queryLocator" type="string" required="false" />
<cfargument name="includeRelatedObject" type="boolean" required="false" default="true" />
<cfargument name="batchSize" type="numeric" required="false" default="200" hint="Minimum of 200. Maximum of 2,000." />
<cfargument name="startRow" type="numeric" required="false" default="#(arguments.batchSize+1)#" />
<cfset var stSOAPArgs = structNew() />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = '' />
<cftry>
<cfset validate() />
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:queryMore>
<urn:queryLocator>#arguments.queryLocator#-#(arguments.startRow-1)#</urn:queryLocator>
</urn:queryMore>
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<cfif arguments.batchSize gt 0>
<cfset stSOAPArgs.appendToHeader = buildQueryOptions(batchsize = arguments.batchSize) />
</cfif>
<cfset stSOAPArgs.requestURL = getServerURL() />
<cfset stSOAPArgs.requestSOAP = stLocal.requestSOAP />
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(argumentCollection = stSOAPArgs) />
<!--- check for valid response --->
<cfset stLocal.result = xmlSearch(stLocal.soapXML, "//*[name()='result']") />
<!--- return soap response when unknown --->
<cfif not arrayLen(stLocal.result)>
<cfthrow message="Unknown response" detail="#trim(arguments.soapRequest.results.fileContent)#" />
</cfif>
<!--- parse query results --->
<cfset stLocal.queryResults = parseQueryXML(stLocal.soapXML, arguments.includeRelatedObject, arguments.batchSize) />
<cfif not stLocal.queryResults.success>
<cfthrow object="#stLocal.queryResults.error#" />
</cfif>
<cfset stReturn = stLocal.queryResults />
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="saveObject" access="public" output="false" returntype="struct">
<cfargument name="objectType" type="string" required="false" />
<cfargument name="objectDataStruct" type="struct" required="false" />
<cfargument name="soapAction" type="string" required="true" />
<cfargument name="sendNull" type="boolean" required="false" default="false" />
<cfargument name="appendAssignmentHeader" type="boolean" required="false" default="false" />
<cfargument name="assignmentHeaderId" type="string" required="false" />
<cfargument name="useDefaultRule" type="boolean" required="false" />
<cfargument name="objectArray" type="array" required="false" />
<cfset var stSOAPArgs = structNew() />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cftry>
<cfset validate() />
<cfif structKeyExists(arguments,'objectDataStruct') and structKeyExists(arguments,'objectType')>
<cfset arguments.objectArray = arrayNew(1) />
<cfset arguments.objectArray[1] = arguments.objectDataStruct />
<cfset arguments.objectArray[1]['_objectType'] = arguments.objectType />
</cfif>
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:#arguments.soapAction#>
<cfloop from="1" to="#arrayLen(arguments.objectArray)#" index="stLocal.iObject">
<urn:sObjects xsi:type="urn1:#arguments.objectArray[stLocal.iObject]._objectType#">
<cfloop collection="#arguments.objectArray[stLocal.iObject]#" item="stLocal.iObjectField">
<cfif not stLocal.iObjectField eq '_objectType' and len(arguments.objectArray[stLocal.iObject][stLocal.iObjectField]) or arguments.sendNull>
<#stLocal.iObjectField#>#xmlFormat(arguments.objectArray[stLocal.iObject][stLocal.iObjectField])#</#stLocal.iObjectField#>
</cfif>
</cfloop>
</urn:sObjects>
</cfloop>
</urn:#arguments.soapAction#>
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<!--- check for assignment header --->
<cfif arguments.appendAssignmentHeader>
<cfset stSOAPArgs.appendToHeader = buildAssignmentHeader(argumentCollection = arguments) />
</cfif>
<cfset stSOAPArgs.requestURL = getServerURL() />
<cfset stSOAPArgs.requestSOAP = stLocal.requestSOAP />
<cfset stSOAPArgs.appendToEnvelope = 'xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' />
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(argumentCollection = stSOAPArgs) />
<!--- check for error codes --->
<cfset stLocal.success = xmlSearch(stLocal.soapXML, "//*[name()='success']") />
<cfset stLocal.statusCode = xmlSearch(stLocal.soapXML, "//*[name()='statusCode']") />
<cfset stLocal.message = xmlSearch(stLocal.soapXML, "//*[name()='message']") />
<!--- on fail grab fault code and info --->
<cfif arrayLen(stLocal.success) and not stLocal.success[1].xmlText and arraylen(stLocal.statusCode) and arraylen(stLocal.message)>
<cfthrow message="#stLocal.message[1].xmlText#" detail="#stLocal.statusCode[1].xmlText#" />
<!--- on success check for success and leadId --->
<cfelse>
<!--- look for multiple results --->
<cfset stLocal.results = xmlSearch(stLocal.soapXML, "//*[name()='result']") />
<cfif arrayLen(stLocal.results)>
<cfset stReturn.results = arrayNew(1) />
<cfloop from="1" to="#arrayLen(stLocal.results)#" index="stLocal.iResult">
<cfset stLocal.result = structNew() />
<cfset stLocal.result.success = stLocal.results[stLocal.iResult]['success'].xmlText />
<!--- return id or error message --->
<cfif stLocal.result.success>
<cfset stLocal.result.idLong = stLocal.results[stLocal.iResult]['id'].xmlText />
<cfset stLocal.result.id = left(stLocal.result.idLong,15) />
<cfset stLocal.result.idURL = getPortalURL() & '/' & stLocal.result.id />
<cfelse>
<cfset stLocal.message = xmlSearch(stLocal.soapXML, "//*[name()='message']") />
<cfset stLocal.result.message = stLocal.message[1].xmlText />
</cfif>
<cfset arrayAppend(stReturn.results,stLocal.result) />
</cfloop>
<cfif arrayLen(stLocal.results) eq 1>
<cfset structAppend(stReturn,stLocal.result) />
</cfif>
<cfelse>
<cfthrow message="Unknown response" detail="#trim(stLocal.soapRequest.results.fileContent)#" />
</cfif>
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="deleteObject" access="public" output="false" returntype="struct">
<cfargument name="idList" type="string" required="true" />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = structNew() />
<cftry>
<cfset validate() />
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:delete>
<cfloop list="#arguments.idList#" index="stLocal.iId">
<urn:ids>#stLocal.iId#</urn:ids>
</cfloop>
</urn:delete>
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(requestURL = getServerURL(), requestSOAP = stLocal.requestSOAP) />
<!--- parse out result --->
<cfset stLocal.result = xmlSearch(stLocal.soapXML, "//*[name()='result']") />
<!--- if valid response set values --->
<cfif arraylen(stLocal.result)>
<!--- create list of columns for query --->
<cfset stLocal.fieldList = structNew() />
<!--- loop thru records to build query --->
<cfloop from="1" to="#arrayLen(stLocal.result[1].xmlChildren)#" index="stLocal.iColumn">
<cfset stLocal.fieldList[listLast(stLocal.result[1].xmlChildren[stLocal.iColumn].xmlName,':')] = '' />
</cfloop>
<cfset stReturn.results = queryNew(structKeyList(stLocal.fieldList)) />
<!--- loop thru field children to build info into struct --->
<cfloop from="1" to="#arrayLen(stLocal.result)#" index="stLocal.iRecord">
<cfset queryAddRow(stReturn.results) />
<!--- loop thru records to build query --->
<cfloop from="1" to="#arrayLen(stLocal.result[stLocal.iRecord].xmlChildren)#" index="stLocal.iColValue">
<cfset querySetCell(stReturn.results,listLast(stLocal.result[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlName,':'),stLocal.result[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlText) />
</cfloop>
</cfloop>
<!--- on unknown return response --->
<cfelse>
<cfthrow message="Unknown response" detail="#trim(stLocal.soapRequest.results.fileContent)#" />
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<!--- SF DESCRIBE CALLS --->
<cffunction name="describeGlobal" access="public" output="false" returntype="struct">
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = arrayNew(1) />
<cftry>
<cfset validate() />
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:describeGlobal />
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(requestURL = getServerURL(), requestSOAP = stLocal.requestSOAP) />
<!--- parse out types --->
<cfset stLocal.types = xmlSearch(stLocal.soapXML, "//*[name()='types']") />
<!--- if valid response set values --->
<cfif arraylen(stLocal.types)>
<cfloop from="1" to="#arrayLen(stLocal.types)#" index="stLocal.iType">
<cfset stReturn.results[stLocal.iType] = stLocal.types[stLocal.iType].xmlText />
</cfloop>
<!--- on unknown return response --->
<cfelse>
<cfthrow message="Unknown response" detail="#trim(stLocal.soapRequest.results.fileContent)#" />
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="describeObject" access="public" output="false" returntype="struct">
<cfargument name="objectType" type="string" required="true" />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = structNew() />
<cftry>
<cfset validate() />
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:describeSObject>
<urn:sObjectType>#arguments.objectType#</urn:sObjectType>
</urn:describeSObject>
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(requestURL = getServerURL(), requestSOAP = stLocal.requestSOAP) />
<!--- parse out fields from xml --->
<cfset stLocal.fields = xmlSearch(stLocal.soapXML, "//*[name()='fields']") />
<!--- if valid response set values --->
<cfif arraylen(stLocal.fields)>
<!--- loop thru field children to build info into struct --->
<cfloop from="1" to="#arrayLen(stLocal.fields)#" index="stLocal.iField">
<cfset stLocal.stChildren = structNew() />
<cfloop from="1" to="#arrayLen(stLocal.fields[stLocal.iField].xmlChildren)#" index="stLocal.iChild">
<cfset stLocal.stChildren[stLocal.fields[stLocal.iField].xmlChildren[stLocal.iChild].xmlName] = stLocal.fields[stLocal.iField].xmlChildren[stLocal.iChild].xmlText />
</cfloop>
<cfset stReturn.results[stLocal.stChildren['name']] = stLocal.stChildren />
</cfloop>
<!--- on unknown return response --->
<cfelse>
<cfthrow message="Unknown response" detail="#trim(stLocal.soapRequest.results.fileContent)#" />
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<!--- SF UTILITY CALLS --->
<cffunction name="getUserInfo" access="public" output="false" returntype="struct">
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = structNew() />
<cftry>
<cfset validate() />
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:getUserInfo />
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(requestURL = getServerURL(), requestSOAP = stLocal.requestSOAP) />
<!--- parse out fields from xml --->
<cfset stLocal.result = xmlSearch(stLocal.soapXML, "//*[name()='result']") />
<cfset stLocal.result = stLocal.result[1].xmlChildren />
<!--- if valid response set values --->
<cfif arraylen(stLocal.result)>
<!--- loop thru field children to build info into struct --->
<cfloop from="1" to="#arrayLen(stLocal.result)#" index="stLocal.iResult">
<cfset structInsert(stReturn.results,stLocal.result[stLocal.iResult].xmlName,stLocal.result[stLocal.iResult].xmlText) />
</cfloop>
<!--- on unknown return response --->
<cfelse>
<cfthrow message="Unknown response" detail="#trim(stLocal.soapRequest.results.fileContent)#" />
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="getServerTimestamp" access="public" output="false" returntype="struct">
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = structNew() />
<cftry>
<cfset validate() />
<!--- build SOAP request--->
<cfsavecontent variable="stLocal.requestSOAP">
<cfoutput>
<soapenv:Body>
<urn:getServerTimestamp />
</soapenv:Body>
</cfoutput>
</cfsavecontent>
<!--- send SOAP request --->
<cfset stLocal.soapXML = brokerSOAPRequest(requestURL = getServerURL(), requestSOAP = stLocal.requestSOAP) />
<!--- parse out fields from xml --->
<cfset stLocal.result = xmlSearch(stLocal.soapXML, "//*[name()='result']") />
<!--- if valid response set values --->
<cfif arraylen(stLocal.result) and arrayLen(stLocal.result[1].xmlChildren) and len(stLocal.result[1].xmlChildren[1].xmlText)>
<!--- loop thru field children to build info into struct --->
<cfset stReturn.results = stLocal.result[1].xmlChildren[1].xmlText />
<!--- on unknown return response --->
<cfelse>
<cfthrow message="Unknown response" detail="#trim(stLocal.soapRequest.results.fileContent)#" />
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<!--- SOAP --->
<cffunction name="sendSoapRequest" access="private" output="false" returntype="struct">
<cfargument name="requestURL" type="string" required="true" />
<cfargument name="requestSOAP" type="string" required="true" />
<cfargument name="appendToEnvelope" type="string" required="false" default="" />
<cfargument name="generateHeader" type="boolean" required="false" default="true" />
<cfargument name="appendToHeader" type="string" required="false" default="" />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = '' />
<cftry>
<cfif arguments.generateHeader>
<!--- build SOAP request--->
<cfsavecontent variable="arguments.requestSOAP">
<cfoutput>
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:enterprise.soap.sforce.com" <cfif len(arguments.appendToEnvelope)>#arguments.appendToEnvelope#</cfif>>
<soapenv:Header>
<urn:SessionHeader>
<urn:sessionId>#getSessionId()#</urn:sessionId>
</urn:SessionHeader>
<cfif len(arguments.appendToHeader)>#arguments.appendToHeader#</cfif>
</soapenv:Header>
#arguments.requestSOAP#
</soapenv:Envelope>
</cfoutput>
</cfsavecontent>
</cfif>
<!--- send SOAP request via http --->
<cfhttp url="#arguments.requestURL#" method="post" charset="utf-8" result="stReturn.results" throwonerror="false" timeout="#getSoapTimeout()#">
<cfhttpparam type="Header" name="Cache-Control" value="no-cache">
<cfhttpparam type="Header" name="Pragma" value="no-cache">
<cfhttpparam type="Header" name="SOAPAction" value="dummy">
<cfhttpparam type="Header" name="Content-Length" value="#len(trim(arguments.requestSOAP))#">
<cfhttpparam type="xml" name="body" value="#trim(arguments.requestSOAP)#" />
</cfhttp>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="formatSoapResponse" access="private" output="false" returntype="struct">
<cfargument name="soapRequest" type="struct" required="true" />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = '' />
<cftry>
<!--- if request fails throw --->
<cfif not arguments.soapRequest.success>
<cfthrow message="#arguments.soapRequest.error.message#" detail="#arguments.soapRequest.error.detail#" />
</cfif>
<!--- parse SOAP request as XML --->
<cfset stReturn.results = xmlParse(arguments.soapRequest.results.fileContent) />
<!--- check for error codes --->
<cfset stLocal.faultCode = xmlSearch(stReturn.results, "//*[name()='faultcode']") />
<cfset stLocal.faultString = xmlSearch(stReturn.results, "//*[name()='faultstring']") />
<!--- on fail grab fault code and info --->
<cfif arraylen(stLocal.faultCode) and arraylen(stLocal.faultString)>
<cfthrow message="#stLocal.faultCode[1].xmlText#" detail="#stLocal.faultString[1].xmlText#" />
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="brokerSoapRequest" access="private" output="false" returntype="any">
<cfargument name="requestURL" type="string" required="true" />
<cfargument name="requestSOAP" type="string" required="true" />
<cfargument name="appendToEnvelope" type="string" required="false" default="" />
<cfargument name="generateHeader" type="boolean" required="false" default="true" />
<cfargument name="appendToHeader" type="string" required="false" default="" />
<cfset var stLocal = structNew() />
<!--- send SOAP request --->
<cfset stLocal.soapRequest = sendSoapRequest(requestURL = arguments.requestURL,
requestSOAP = arguments.requestSOAP,
appendToEnvelope = arguments.appendToEnvelope,
generateHeader = arguments.generateHeader,
appendToHeader = arguments.appendToHeader) />
<!--- format SOAP response --->
<cfset stLocal.soapResponse = formatSoapResponse(stLocal.soapRequest) />
<!--- if invalid session error and auto login is enabled --->
<cfif not stLocal.soapResponse.success and findNoCase('INVALID_SESSION_ID',stLocal.soapResponse.error.detail) and getAutoLogin()>
<!--- attempt login --->
<cfset stLocal.stLogin = login() />
<!--- throw when service is down to invalid credentials --->
<cfif not stLocal.stLogin.success>
<cfthrow object="#stLocal.stLogin.error#" />
</cfif>
<!--- re-send original request on successful login --->
<cfset stLocal.soapRequest = sendSoapRequest(requestURL = arguments.requestURL,
requestSOAP = arguments.requestSOAP,
appendToEnvelope = arguments.appendToEnvelope,
generateHeader = arguments.generateHeader,
appendToHeader = arguments.appendToHeader) />
<cfset stLocal.soapResponse = formatSoapResponse(stLocal.soapRequest) />
</cfif>
<!--- throw on error from format response --->
<cfif not stLocal.soapResponse.success>
<cfthrow object="#stLocal.soapResponse.error#" />
</cfif>
<cfreturn stLocal.soapResponse.results />
</cffunction>
<!--- HELPERS --->
<cffunction name="parseQueryXML" access="private" output="false" returntype="struct">
<cfargument name="soapXML" type="xml" required="true" />
<cfargument name="includeRelatedObject" type="boolean" required="true" />
<cfargument name="batchSize" type="numeric" required="true" />
<cfset var stLocal = structNew() />
<cfset var stReturn = structNew() />
<cfset stReturn.success = true />
<cfset stReturn.results = '' />
<cftry>
<!--- get query size --->
<cfset stLocal.totalRecords = xmlSearch(arguments.soapXML, "//*[name()='ns1:size']") />
<!--- return size of query --->
<cfset stReturn.size = val(stLocal.totalRecords[1].XmlText)>
<!--- if valid size set values --->
<cfif arrayLen(stLocal.totalRecords)>
<cfset stLocal.records = xmlSearch(arguments.soapXML, "//*[name()='ns1:records']") />
<!--- if records found build query --->
<cfif arraylen(stLocal.records)>
<!--- create list of columns for query --->
<cfset stLocal.fieldList = structNew() />
<!--- loop thru records to build query --->
<cfloop from="1" to="#arrayLen(stLocal.records[1].xmlChildren)#" index="stLocal.iColumn">
<cfset stLocal.fieldList[listLast(stLocal.records[1].xmlChildren[stLocal.iColumn].xmlName,':')] = '' />
</cfloop>
<cfset stReturn.results = queryNew(structKeyList(stLocal.fieldList)) />
<!--- loop thru field children to build info into struct --->
<cfloop from="1" to="#arrayLen(stLocal.records)#" index="stLocal.iRecord">
<cfset queryAddRow(stReturn.results) />
<!--- loop thru records to build query --->
<cfloop from="1" to="#arrayLen(stLocal.records[stLocal.iRecord].xmlChildren)#" index="stLocal.iColValue">
<!--- if query contains data from related object --->
<cfif structKeyExists(stLocal.records[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlAttributes,'xsi:type')>
<!--- create list of columns to add to query --->
<cfset stLocal.subFieldList = structNew() />
<!--- load fields and vals into struct --->
<cfloop from="1" to="#arrayLen(stLocal.records[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlChildren)#" index="stLocal.iSubColumn">
<cfset stLocal.subFieldList[listLast(stLocal.records[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlChildren[stLocal.iSubColumn].xmlName,':')] = stLocal.records[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlChildren[stLocal.iSubColumn].xmlText />
</cfloop>
<!--- build struct into query --->
<cfif arguments.includeRelatedObject>
<cfset querySetCell(stReturn.results,listLast(stLocal.records[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlName,':'),stLocal.subFieldList) />
</cfif>
<!--- build struct fields into query as columns --->
<cfloop list="#structKeyList(stLocal.subFieldList)#" index="stLocal.iField">
<!--- if prefix realted fields - append object name to field --->
<cfset stLocal.subFieldName = listLast(stLocal.records[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlName,':') & '_' & stLocal.iField />
<cfif not listFindNoCase(stReturn.results.columnList,stLocal.subFieldName)>
<cfset queryAddColumn(stReturn.results,stLocal.subFieldName,arrayNew(1)) />
</cfif>
<cfset querySetCell(stReturn.results,stLocal.subFieldName,stLocal.subFieldList[stLocal.iField]) />
</cfloop>
<cfelse>
<cfset querySetCell(stReturn.results,listLast(stLocal.records[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlName,':'),stLocal.records[stLocal.iRecord].xmlChildren[stLocal.iColValue].xmlText) />
</cfif>
</cfloop>
</cfloop>
</cfif>
<!--- check for query locator --->
<cfset stLocal.aQueryLocator = xmlSearch(arguments.soapXML, "//*[name()='ns1:queryLocator']") />
<cfif arrayLen(stLocal.aQueryLocator) and listLen(stLocal.aQueryLocator[1].xmlText,'-')>
<cfset stReturn.queryLocator = listFirst(stLocal.aQueryLocator[1].xmlText,'-') />
<cfset stReturn.locatorChunk = listLast(stLocal.aQueryLocator[1].xmlText,'-') />
<cfset stReturn.startRow = (stReturn.locatorChunk - arguments.batchSize) + 1 />
<cfset stReturn.batchSize = arguments.batchSize />
</cfif>
</cfif>
<!--- trap - return failure and error --->
<cfcatch type="any">
<cfset stReturn.success = false />
<cfset stReturn.error = cfcatch />
</cfcatch>
</cftry>
<cfreturn stReturn />
</cffunction>
<cffunction name="validate" access="private" output="false" returntype="boolean">
<cfif not structKeyExists(variables,'instance')>
<cfthrow message="Object not initialized" detail="Create an instance of object with init() method" type="salesForceCFC" />
<cfelseif not len(getSessionId())>
<cfthrow message="Invalid Session Id" detail="Session Id is empty. Use login() method." type="salesForceCFC" />
<cfelseif not len(getServerURL())>
<cfthrow message="Invalid Server URL" detail="Server URL is empty. Use login() method." type="salesForceCFC" />
</cfif>
<cfreturn true />
</cffunction>
<cffunction name="buildAssignmentHeader" access="private" output="false" returntype="string">
<cfargument name="assignmentHeaderId" type="string" required="false" />
<cfargument name="useDefaultRule" type="boolean" required="false" />
<cfset var assignmentHeader = '' />
<cfsavecontent variable="assignmentHeader">
<cfoutput>
<urn:AssignmentRuleHeader>
<cfif structKeyExists(arguments,'assignmentRuleId')>
<urn:assignmentRuleId>#arguments.assignmentHeaderId#</urn:assignmentRuleId>
<cfelseif structKeyExists(arguments,'useDefaultRule')>
<urn:useDefaultRule>#arguments.useDefaultRule#</urn:useDefaultRule>
</cfif>
</urn:AssignmentRuleHeader>
</cfoutput>
</cfsavecontent>
<cfreturn trim(assignmentHeader) />
</cffunction>
<cffunction name="buildEmailHeader" access="private" output="false" returntype="string">
<cfargument name="triggerAutoResponseEmail" type="boolean" required="false" />
<cfargument name="triggerOtherEmail" type="boolean" required="false" />
<cfargument name="triggerUserEmail" type="boolean" required="false" />
<cfset var emailHeader = '' />
<cfsavecontent variable="emailHeader">
<cfoutput>
<urn:EmailHeader>
<cfif structKeyExists(arguments,'triggerAutoResponseEmail')>
<urn:triggerAutoResponseEmail>#arguments.triggerAutoResponseEmail#</urn:triggerAutoResponseEmail>
<cfelseif structKeyExists(arguments,'triggerOtherEmail')>
<urn:triggerOtherEmail>#arguments.triggerOtherEmail#</urn:triggerOtherEmail>
<cfelseif structKeyExists(arguments,'triggerUserEmail')>
<urn:triggerUserEmail>#arguments.triggerUserEmail#</urn:triggerUserEmail>
</cfif>
</urn:EmailHeader>
</cfoutput>
</cfsavecontent>
<cfreturn trim(emailHeader) />
</cffunction>
<cffunction name="buildQueryOptions" access="private" output="false" returntype="string">
<cfargument name="batchSize" type="numeric" required="true" />
<cfset var queryHeader = '' />
<cfsavecontent variable="queryHeader">
<cfoutput>
<urn:QueryOptions>
<urn:batchSize>#arguments.batchSize#</urn:batchSize>
</urn:QueryOptions>
</cfoutput>
</cfsavecontent>
<cfreturn trim(queryHeader) />
</cffunction>
</cfcomponent>