Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit be5cdd4

Browse files
author
Pat Patterson
committed
Accept String or correctly typed arguments in create, update, upsert. Fixes #33
1 parent eb51ef3 commit be5cdd4

File tree

2 files changed

+106
-70
lines changed

2 files changed

+106
-70
lines changed

RemoteTKController.cls

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,55 @@ public class RemoteTKController {
3737
return gen.getAsString();
3838
}
3939

40+
private static String writeFields(String objtype, SObject obj, String fields) {
41+
Map<String, Object> fieldMap = null;
42+
try {
43+
fieldMap = (Map<String, Object>)JSON.deserializeUntyped(fields);
44+
} catch (JSONException je) {
45+
return makeError(je.getMessage(), 'JSON_PARSER_ERROR');
46+
}
47+
48+
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objtype);
49+
50+
Map<String, Schema.sObjectField> targetFields = targetType.getDescribe().fields.getMap();
51+
52+
try {
53+
for (String key : fieldMap.keySet()) {
54+
if (targetFields.get(key) == null) {
55+
return '[{"message":"Field '+key+' does not exist on object type '+objtype+'","errorCode":"INVALID_FIELD"}]';
56+
}
57+
58+
Object value = fieldMap.get(key);
59+
Schema.DisplayType valueType = targetFields.get(key).getDescribe().getType();
60+
61+
if (value instanceof String && valueType != Schema.DisplayType.String) {
62+
// Coerce an incoming String to the correct type
63+
String svalue = (String)value;
64+
65+
if (valueType == Schema.DisplayType.Date) {
66+
obj.put(key, Date.valueOf(svalue));
67+
} else if (valueType == Schema.DisplayType.Percent ||
68+
valueType == Schema.DisplayType.Currency) {
69+
obj.put(key, svalue == '' ? null : Decimal.valueOf(svalue));
70+
} else if (valueType == Schema.DisplayType.Double) {
71+
obj.put(key, svalue == '' ? null : Double.valueOf(svalue));
72+
} else if (valueType == Schema.DisplayType.Integer) {
73+
obj.put(key, Integer.valueOf(svalue));
74+
} else {
75+
obj.put(key, svalue);
76+
}
77+
} else {
78+
// Just try putting the incoming value on the object
79+
obj.put(key, value);
80+
}
81+
}
82+
} catch (SObjectException soe) {
83+
return makeError(soe.getMessage(), 'INVALID_FIELD');
84+
}
85+
86+
return null;
87+
}
88+
4089
@remoteAction
4190
public static String describe(String objtype) {
4291
// Just enough to make the sample app work!
@@ -82,39 +131,12 @@ public class RemoteTKController {
82131
}
83132

84133
SObject obj = targetType.newSObject();
85-
86-
Map<String, Object> fieldMap = null;
87-
try {
88-
fieldMap = (Map<String, Object>)JSON.deserializeUntyped(fields);
89-
} catch (JSONException je) {
90-
return makeError(je.getMessage(), 'JSON_PARSER_ERROR');
91-
}
92-
93-
Map<String, Schema.sObjectField> targetFields = targetType.getDescribe().fields.getMap();
94-
95-
try {
96-
for (String key : fieldMap.keySet()) {
97-
if (targetFields.get(key) == null) {
98-
return '[{"message":"Field '+key+' does not exist on object type '+objtype+'","errorCode":"INVALID_FIELD"}]';
99-
}
100-
101-
if (targetFields.get(key).getDescribe().getType() == Schema.DisplayType.Date) {
102-
obj.put(key, Date.valueOf((String)fieldMap.get(key)));
103-
} else if (targetFields.get(key).getDescribe().getType() == Schema.DisplayType.Percent ||
104-
targetFields.get(key).getDescribe().getType() == Schema.DisplayType.Currency) {
105-
obj.put(key, String.valueOf(fieldMap.get(key)) == '' ? null : Decimal.valueOf((String)fieldMap.get(key)));
106-
} else if (targetFields.get(key).getDescribe().getType() == Schema.DisplayType.Double) {
107-
obj.put(key, String.valueOf(fieldMap.get(key)) == '' ? null : Double.valueOf(fieldMap.get(key)));
108-
} else if (targetFields.get(key).getDescribe().getType() == Schema.DisplayType.Integer) {
109-
obj.put(key, Integer.valueOf(fieldMap.get(key)));
110-
} else {
111-
obj.put(key, fieldMap.get(key));
112-
}
113-
}
114-
} catch (SObjectException soe) {
115-
return makeError(soe.getMessage(), 'INVALID_FIELD');
134+
135+
String error = writeFields(objType, obj, fields);
136+
if (error != null) {
137+
return error;
116138
}
117-
139+
118140
try {
119141
insert obj;
120142
} catch (DMLException dmle) {
@@ -172,14 +194,9 @@ public class RemoteTKController {
172194
SObject obj = targetType.newSObject();
173195
obj.put(externalIdField, externalId);
174196

175-
Map<String, Object> fieldMap =
176-
(Map<String, Object>)JSON.deserializeUntyped(fields);
177-
try {
178-
for (String key : fieldMap.keySet()) {
179-
obj.put(key, fieldMap.get(key));
180-
}
181-
} catch (SObjectException soe) {
182-
return makeError(soe.getMessage(), 'INVALID_FIELD');
197+
String error = writeFields(objType, obj, fields);
198+
if (error != null) {
199+
return error;
183200
}
184201

185202
Schema.SObjectField sobjField = targetType.getDescribe().fields.getMap().get(externalIdField);
@@ -198,21 +215,11 @@ public class RemoteTKController {
198215

199216
SObject obj = targetType.newSObject(id);
200217

201-
Map<String, Object> fieldMap = null;
202-
try {
203-
fieldMap = (Map<String, Object>)JSON.deserializeUntyped(fields);
204-
} catch (JSONException je) {
205-
return makeError(je.getMessage(), 'JSON_PARSER_ERROR');
218+
String error = writeFields(objType, obj, fields);
219+
if (error != null) {
220+
return error;
206221
}
207222

208-
try {
209-
for (String key : fieldMap.keySet()) {
210-
obj.put(key, fieldMap.get(key));
211-
}
212-
} catch (SObjectException soe) {
213-
return makeError(soe.getMessage(), 'INVALID_FIELD');
214-
}
215-
216223
try {
217224
update obj;
218225
} catch (DMLException dmle) {

TestRemoteTKController.cls

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ public class TestRemoteTKController{
8888
'Wrong account number in '+method+' result');
8989
}
9090

91-
static private Id testCreate(String accName, String accNumber) {
91+
static private Id testCreate(String accName, String accNumber, String fields) {
9292
// Assume we can create an account
93-
String jsonResult = RemoteTKController.create('Account', '{"Name": "'+accName+'", "AccountNumber" : "'+accNumber+'"}');
93+
94+
// Try with data in correct types
95+
String jsonResult = RemoteTKController.create('Account',
96+
'{"Name": "'+accName+'", '+
97+
'"AccountNumber" : "'+accNumber+'",'+
98+
fields+'}');
9499

95100
System.assertNotEquals(null, jsonResult,
96101
'RemoteTKController.create returned null');
@@ -203,22 +208,22 @@ public class TestRemoteTKController{
203208
assertError(jsonResult, 'INVALID_SEARCH', 'RemoteTKController.search');
204209
}
205210

206-
static private void testUpdate(String accName, String accNumber, Id id) {
207-
String jsonResult = RemoteTKController.updat('Account', id, '{"Name":"'+accName+'1", "AccountNumber":"'+accNumber+'1"}');
211+
static private void testUpdate(String accName, String accNumber, Id id, String fields) {
212+
String jsonResult = RemoteTKController.updat('Account', id, '{"Name":"'+accName+'", "AccountNumber":"'+accNumber+'"}');
208213
System.assertEquals(null, jsonResult,
209214
'Non-null result from RemoteTKController.updat');
210215
Account account = [SELECT Id, Name, AccountNumber FROM Account WHERE Id = :id LIMIT 1];
211216
System.assertNotEquals(null, account,
212217
'Couldn\'t find account record after RemoteTKController.updat');
213-
System.assertEquals(accName+'1', account.Name,
218+
System.assertEquals(accName, account.Name,
214219
'Account name doesn\'t match after RemoteTKController.updat');
215-
System.assertEquals(accNumber+'1', account.AccountNumber,
220+
System.assertEquals(accNumber, account.AccountNumber,
216221
'Account number doesn\'t match after RemoteTKController.updat');
217222

218-
jsonResult = RemoteTKController.updat('QXZXQZXZQXZQ', id, '{"Name":"'+accName+'1"}');
223+
jsonResult = RemoteTKController.updat('QXZXQZXZQXZQ', id, '{"Name":"'+accName+'"}');
219224
assertError(jsonResult, 'NOT_FOUND', 'RemoteTKController.updat');
220225

221-
jsonResult = RemoteTKController.updat('Account', id, '{"XQZXQZXQZXQZ" : "'+accName+'1"}');
226+
jsonResult = RemoteTKController.updat('Account', id, '{"XQZXQZXQZXQZ" : "'+accName+'"}');
222227
assertError(jsonResult, 'INVALID_FIELD', 'RemoteTKController.updat');
223228

224229
jsonResult = RemoteTKController.updat('Account', id, '{"Name" "'+accName+'"}');
@@ -228,22 +233,27 @@ public class TestRemoteTKController{
228233
assertError(jsonResult, 'STRING_TOO_LONG', 'RemoteTKController.updat');
229234
}
230235

231-
static private void testUpsert(String accName, String accNumber, Id id) {
232-
String jsonResult = RemoteTKController.upser('Account', 'Id', (String)id, '{"Name":"'+accName+'2", "AccountNumber":"'+accNumber+'2"}');
236+
static private void testUpsert(String accName, String accNumber, Id id, String fields) {
237+
String jsonResult = RemoteTKController.upser('Account',
238+
'Id',
239+
(String)id,
240+
'{"Name":"'+accName+'", '+
241+
'"AccountNumber":"'+accNumber+'",'+
242+
fields+'}');
233243
System.assertEquals(null, jsonResult,
234244
'Non-null result from RemoteTKController.upser');
235245
Account account = [SELECT Id, Name, AccountNumber FROM Account WHERE Id = :id LIMIT 1];
236246
System.assertNotEquals(null, account,
237247
'Couldn\'t find account record after RemoteTKController.upser');
238-
System.assertEquals(accName+'2', account.Name,
248+
System.assertEquals(accName, account.Name,
239249
'Account name doesn\'t match after RemoteTKController.upser');
240-
System.assertEquals(accNumber+'2', account.AccountNumber,
250+
System.assertEquals(accNumber, account.AccountNumber,
241251
'Account number doesn\'t match after RemoteTKController.upser');
242252

243-
jsonResult = RemoteTKController.upser('QXZXQZXZQXZQ', 'Id', (String)id, '{"Name":"'+accName+'2"}');
253+
jsonResult = RemoteTKController.upser('QXZXQZXZQXZQ', 'Id', (String)id, '{"Name":"'+accName+'"}');
244254
assertError(jsonResult, 'NOT_FOUND', 'RemoteTKController.upser');
245255

246-
jsonResult = RemoteTKController.upser('Account', 'Id', (String)id, '{"XQZXQZXQZXQZ" : "'+accName+'2"}');
256+
jsonResult = RemoteTKController.upser('Account', 'Id', (String)id, '{"XQZXQZXQZXQZ" : "'+accName+'"}');
247257
assertError(jsonResult, 'INVALID_FIELD', 'RemoteTKController.upser');
248258
}
249259

@@ -266,12 +276,31 @@ public class TestRemoteTKController{
266276
String accName = 'Test1';
267277
String accNumber = '1234';
268278

269-
Id id = testCreate(accName, accNumber);
279+
// String field values
280+
Id id = testCreate(accName, accNumber, '"AnnualRevenue" : "1000000",'+
281+
'"NumberOfEmployees" : "1000",'+
282+
'"Phone" : "(111) 222-3333"');
283+
testDelete(id);
284+
285+
// Integer field values
286+
id = testCreate(accName, accNumber, '"AnnualRevenue" : 1000000,'+
287+
'"NumberOfEmployees" : 1000,'+
288+
'"Phone" : "(111) 222-3333"');
270289
testRetrieve(accName, accNumber, id);
271290
testQuery(accName, accNumber);
272291
testSearch(accName, accNumber, id);
273-
testUpdate(accName, accNumber, id);
274-
testUpsert(accName, accNumber, id);
292+
testUpdate(accName+'1', accNumber+'1', id, '"AnnualRevenue" : "1100000",'+
293+
'"NumberOfEmployees" : "1100",'+
294+
'"Phone" : "(112) 222-3333"');
295+
testUpdate(accName+'2', accNumber+'2', id, '"AnnualRevenue" : "2000000",'+
296+
'"NumberOfEmployees" : "2000",'+
297+
'"Phone" : "(222) 222-3333"');
298+
testUpsert(accName+'3', accNumber+'3', id, '"AnnualRevenue" : 3000000,'+
299+
'"NumberOfEmployees" : 3000,'+
300+
'"Phone" : "(333) 222-3333"');
301+
testUpsert(accName+'4', accNumber+'4', id, '"AnnualRevenue" : 4000000,'+
302+
'"NumberOfEmployees" : 4000,'+
303+
'"Phone" : "(444) 222-3333"');
275304
testDelete(id);
276305
}
277306
}

0 commit comments

Comments
 (0)