FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Force.com-JavaScript-REST-Toolkit/RemoteTKController.cls at master · singharjun/Force.com-JavaScript-REST-Toolkit · GitHub
singharjun
/
Force.com-JavaScript-REST-Toolkit
Public
forked from
developerforce/Force.com-JavaScript-REST-Toolkit
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Force.com-JavaScript-REST-Toolkit
/
RemoteTKController.cls
Copy path
More file actions
More file actions
Latest commit
History
History
History
315 lines (266 loc) · 12.2 KB
Breadcrumbs
Force.com-JavaScript-REST-Toolkit
/
RemoteTKController.cls
Copy path
File metadata and controls
315 lines (266 loc) · 12.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright (c) 2012, salesforce.com, inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
public
class
RemoteTKController
{
private
static
String
makeError
(
String
message
,
String
errorCode
) {
JSONGenerator
gen
=
JSON
.
createGenerator
(
false
);
gen
.
writeStartArray
();
gen
.
writeStartObject
();
gen
.
writeStringField
(
'
message
'
,
message
);
gen
.
writeStringField
(
'
errorCode
'
,
errorCode
);
gen
.
writeEndObject
();
gen
.
writeEndArray
();
return
gen
.
getAsString
();
}
private
static
String
writeFields
(
String
objtype
,
SObject
obj
,
String
fields
) {
Map
<
String
,
Object
>
fieldMap
=
null
;
try
{
fieldMap
=
(
Map
<
String
,
Object
>)
JSON
.
deserializeUntyped
(
fields
);
}
catch
(
JSONException
je
) {
return
makeError
(
je
.
getMessage
(),
'
JSON_PARSER_ERROR
'
);
}
Schema
.
SObjectType
targetType
=
Schema
.
getGlobalDescribe
().
get
(
objtype
);
Map
<
String
,
Schema
.
sObjectField
>
targetFields
=
targetType
.
getDescribe
().
fields
.
getMap
();
try
{
for
(
String
key
:
fieldMap
.
keySet
()) {
if
(
targetFields
.
get
(
key
)
==
null
) {
return
'
[{"message":"Field
'
+
key
+
'
does not exist on object type
'
+
objtype
+
'
","errorCode":"INVALID_FIELD"}]
'
;
}
Object
value
=
fieldMap
.
get
(
key
);
Schema
.
DisplayType
valueType
=
targetFields
.
get
(
key
).
getDescribe
().
getType
();
if
(
value
instanceof
String
&&
valueType
!=
Schema
.
DisplayType
.
String
) {
//
Coerce an incoming String to the correct type
String
svalue
=
(
String
)
value
;
if
(
valueType
==
Schema
.
DisplayType
.
Date
) {
obj
.
put
(
key
,
Date
.
valueOf
(
svalue
));
}
else
if
(
valueType
==
Schema
.
DisplayType
.
Percent
||
valueType
==
Schema
.
DisplayType
.
Currency
) {
obj
.
put
(
key
,
svalue
==
'
'
?
null
:
Decimal
.
valueOf
(
svalue
));
}
else
if
(
valueType
==
Schema
.
DisplayType
.
Double
) {
obj
.
put
(
key
,
svalue
==
'
'
?
null
:
Double
.
valueOf
(
svalue
));
}
else
if
(
valueType
==
Schema
.
DisplayType
.
Integer
) {
obj
.
put
(
key
,
Integer
.
valueOf
(
svalue
));
}
else
{
obj
.
put
(
key
,
svalue
);
}
}
else
{
//
Just try putting the incoming value on the object
obj
.
put
(
key
,
value
);
}
}
}
catch
(
SObjectException
soe
) {
return
makeError
(
soe
.
getMessage
(),
'
INVALID_FIELD
'
);
}
return
null
;
}
private
static
List
<
Object
>
picklistOptions
(Schema.
DescribeFieldResult
field
) {
List
<
Object
>
picklistOptions
=
new
List
<
Object
>();
Schema
.
DisplayType
fieldType
=
field
.
getType
();
if
(
fieldType
!=
Schema
.
DisplayType
.
Picklist
&&
fieldType
!=
Schema
.
DisplayType
.
MultiPicklist
&&
fieldType
!=
Schema
.
DisplayType
.
Combobox
) {
return
picklistOptions
;
}
List
<
Schema
.
PicklistEntry
>
picklistValues
=
field
.
getPicklistValues
();
for
(
Schema
.
PicklistEntry
picklistValue
:
picklistValues
) {
Map
<
String
,
Object
>
picklistOption
=
new
Map
<
String
,
Object
>();
picklistOption
.
put
(
'
value
'
,
picklistValue
.
getValue
());
picklistOption
.
put
(
'
label
'
,
picklistValue
.
getValue
());
picklistOption
.
put
(
'
active
'
,
picklistValue
.
isActive
());
picklistOption
.
put
(
'
defaultValue
'
,
picklistValue
.
isDefaultValue
());
picklistOptions
.
add
(
picklistOption
);
}
return
picklistOptions
;
}
@remoteAction
public
static
String
describe
(
String
objtype
) {
//
Just enough to make the sample app work!
Schema
.
SObjectType
targetType
=
Schema
.
getGlobalDescribe
().
get
(
objtype
);
if
(
targetType
==
null
) {
return
makeError
(
'
The requested resource does not exist
'
,
'
NOT_FOUND
'
);
}
Schema
.
DescribeSObjectResult
sobjResult
=
targetType
.
getDescribe
();
Map
<
String
,
Schema
.
SObjectField
>
fieldMap
=
sobjResult
.
fields
.
getMap
();
List
<
Object
>
fields
=
new
List
<
Object
>();
for
(
String
key
:
fieldMap
.
keySet
()) {
Schema
.
DescribeFieldResult
descField
=
fieldMap
.
get
(
key
).
getDescribe
();
Map
<
String
,
Object
>
field
=
new
Map
<
String
,
Object
>();
field
.
put
(
'
type
'
,
descField
.
getType
().
name
().
toLowerCase
());
field
.
put
(
'
name
'
,
descField
.
getName
());
field
.
put
(
'
label
'
,
descField
.
getLabel
());
List
<
String
>
references
=
new
List
<
String
>();
for
(
Schema
.
sObjectType
t
:
descField
.
getReferenceTo
()) {
references
.
add
(
t
.
getDescribe
().
getName
());
}
if
(
!
references
.
isEmpty
()) {
field
.
put
(
'
referenceTo
'
,
references
);
}
field
.
put
(
'
picklistValues
'
,
picklistOptions
(
descField
));
fields
.
add
(
field
);
}
Map
<
String
,
Object
>
result
=
new
Map
<
String
,
Object
>();
result
.
put
(
'
fields
'
,
fields
);
return
JSON
.
serialize
(
result
);
}
@remoteAction
public
static
String
create
(
String
objtype
,
String
fields
) {
Schema
.
SObjectType
targetType
=
Schema
.
getGlobalDescribe
().
get
(
objtype
);
if
(
targetType
==
null
) {
return
makeError
(
'
The requested resource does not exist
'
,
'
NOT_FOUND
'
);
}
SObject
obj
=
targetType
.
newSObject
();
String
error
=
writeFields
(
objType
,
obj
,
fields
);
if
(
error
!=
null
) {
return
error
;
}
try
{
insert
obj
;
}
catch
(
DMLException
dmle
) {
String
fieldNames
=
'
'
;
for
(
String
field
:
dmle
.
getDmlFieldNames
(
0
)) {
if
(
fieldNames
.
length
()
>
0
) {
fieldNames
+=
'
,
'
;
}
fieldNames
+=
'
"
'
+
field
+
'
"
'
;
}
return
'
[{"fields":[
'
+
fieldNames
+
'
],"message":"
'
+
dmle
.
getDmlMessage
(
0
)
+
'
","errorCode":"
'
+
dmle
.
getDmlType
(
0
).
name
()
+
'
"}]
'
;
}
Map
<
String
,
Object
>
result
=
new
Map
<
String
,
Object
>();
result
.
put
(
'
id
'
,
obj
.
id
);
result
.
put
(
'
errors
'
,
new
List
<
String
>());
result
.
put
(
'
success
'
,
true
);
return
JSON
.
serialize
(
result
);
}
@remoteAction
public
static
String
retrieve
(
String
objtype
,
String
id
,
String
fieldlist
) {
//
TODO - handle null fieldlist - retrieve all fields
Boolean
containsId
=
false
;
for
(
String
field
:
fieldlist
.
split
(
'
,
'
)) {
if
(
field
.
equalsIgnoreCase
(
'
id
'
)){
containsId
=
true
;
break
;
}
}
if
(
!
containsId
) {
fieldlist
=
'
Id,
'
+
fieldlist
;
}
String
soql
=
'
SELECT
'
+
fieldlist
+
'
FROM
'
+
objtype
+
'
WHERE Id =
\'
'
+
id
+
'
\'
'
;
List
<
sObject
>
records
;
try
{
records
=
Database
.
query
(
soql
);
}
catch
(
QueryException
qe
) {
return
makeError
(
qe
.
getMessage
(),
'
INVALID_QUERY
'
);
}
return
JSON
.
serialize
(
records
[
0
]);
}
@remoteAction
public
static
String
upser
(
String
objtype
,
String
externalIdField
,
String
externalId
,
String
fields
) {
Schema
.
SObjectType
targetType
=
Schema
.
getGlobalDescribe
().
get
(
objtype
);
if
(
targetType
==
null
) {
return
makeError
(
'
The requested resource does not exist
'
,
'
NOT_FOUND
'
);
}
SObject
obj
=
targetType
.
newSObject
();
obj
.
put
(
externalIdField
,
externalId
);
String
error
=
writeFields
(
objType
,
obj
,
fields
);
if
(
error
!=
null
) {
return
error
;
}
Schema
.
SObjectField
sobjField
=
targetType
.
getDescribe
().
fields
.
getMap
().
get
(
externalIdField
);
Database
.
Upsert
(
obj
,
sobjField
);
return
null
;
}
@remoteAction
public
static
String
updat
(
String
objtype
,
String
id
,
String
fields
) {
Schema
.
SObjectType
targetType
=
Schema
.
getGlobalDescribe
().
get
(
objtype
);
if
(
targetType
==
null
) {
return
makeError
(
'
The requested resource does not exist
'
,
'
NOT_FOUND
'
);
}
SObject
obj
=
targetType
.
newSObject
(
id
);
String
error
=
writeFields
(
objType
,
obj
,
fields
);
if
(
error
!=
null
) {
return
error
;
}
try
{
update
obj
;
}
catch
(
DMLException
dmle
) {
String
fieldNames
=
'
'
;
for
(
String
field
:
dmle
.
getDmlFieldNames
(
0
)) {
if
(
fieldNames
.
length
()
>
0
) {
fieldNames
+=
'
,
'
;
}
fieldNames
+=
'
"
'
+
field
+
'
"
'
;
}
return
'
[{"fields":[
'
+
fieldNames
+
'
],"message":"
'
+
dmle
.
getDmlMessage
(
0
)
+
'
","errorCode":"
'
+
dmle
.
getDmlType
(
0
).
name
()
+
'
"}]
'
;
}
return
null
;
}
@remoteAction
public
static
String
del
(
String
objtype
,
String
id
) {
Schema
.
SObjectType
targetType
=
Schema
.
getGlobalDescribe
().
get
(
objtype
);
if
(
targetType
==
null
) {
return
makeError
(
'
The requested resource does not exist
'
,
'
NOT_FOUND
'
);
}
SObject
obj
=
targetType
.
newSObject
(
id
);
try
{
delete
obj
;
}
catch
(
DMLException
dmle
) {
String
fieldNames
=
'
'
;
for
(
String
field
:
dmle
.
getDmlFieldNames
(
0
)) {
if
(
fieldNames
.
length
()
>
0
) {
fieldNames
+=
'
,
'
;
}
fieldNames
+=
'
"
'
+
field
+
'
"
'
;
}
return
'
[{"fields":[
'
+
fieldNames
+
'
],"message":"
'
+
dmle
.
getDmlMessage
(
0
)
+
'
","errorCode":"
'
+
dmle
.
getDmlType
(
0
).
name
()
+
'
"}]
'
;
}
return
null
;
}
@remoteAction
public
static
String
query
(
String
soql
) {
List
<
sObject
>
records
;
try
{
records
=
Database
.
query
(
soql
);
}
catch
(
QueryException
qe
) {
return
makeError
(
qe
.
getMessage
(),
'
INVALID_QUERY
'
);
}
Map
<
String
,
Object
>
result
=
new
Map
<
String
,
Object
>();
result
.
put
(
'
records
'
,
records
);
result
.
put
(
'
totalSize
'
,
records
.
size
());
result
.
put
(
'
done
'
,
true
);
return
JSON
.
serialize
(
result
);
}
@remoteAction
public
static
String
search
(
String
sosl
) {
List
<
List
<
SObject
>>
result
;
try
{
result
=
Search
.
query
(
sosl
);
}
catch
(
QueryException
qe
) {
return
makeError
(
qe
.
getMessage
(),
'
INVALID_SEARCH
'
);
}
catch
(
SearchException
se
) {
return
makeError
(
se
.
getMessage
(),
'
INVALID_SEARCH
'
);
}
return
JSON
.
serialize
(
result
);
}
}
Back
|
FazBrowse Home
|
New Git URL