-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactDataController.cls
More file actions
430 lines (413 loc) · 14 KB
/
Copy pathContactDataController.cls
File metadata and controls
430 lines (413 loc) · 14 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
public with sharing class ContactDataController {
private static final Integer ACCOUNT_PAGE_SIZE = 25;
@AuraEnabled
public static ReturnValue getAccountForLookup(
String searchStr,
String lastAccountName,
Id lastAccountId
) {
ReturnValue rt = new ReturnValue();
try {
// Check CRUD/FLS permissions before query
if (!Schema.SObjectType.Account.isAccessible()) {
throw new AuraHandledException(
'ユーザーに取引先の参照権限がありません。'
);
}
String normalizedSearch = String.isBlank(searchStr)
? null
: searchStr.trim();
String searchPattern = normalizedSearch == null
? null
: '%' + normalizedSearch + '%';
Boolean hasCursor =
String.isNotBlank(lastAccountName) && lastAccountId != null;
Integer queryLimit = ACCOUNT_PAGE_SIZE + 1;
List<Account> accountList;
// Keyset pagination keeps loading reliable even when an org has more
// than Salesforce's OFFSET limit. The Id tie-breaker handles duplicate
// Account names without skipping or repeating records.
if (String.isBlank(normalizedSearch) && !hasCursor) {
accountList = [
SELECT Id, Name
FROM Account
WITH SECURITY_ENFORCED
ORDER BY Name ASC, Id ASC
LIMIT :queryLimit
];
} else if (String.isBlank(normalizedSearch)) {
accountList = [
SELECT Id, Name
FROM Account
WHERE
(Name > :lastAccountName
OR (Name = :lastAccountName AND Id > :lastAccountId))
WITH SECURITY_ENFORCED
ORDER BY Name ASC, Id ASC
LIMIT :queryLimit
];
} else if (!hasCursor) {
accountList = [
SELECT Id, Name
FROM Account
WHERE Name LIKE :searchPattern
WITH SECURITY_ENFORCED
ORDER BY Name ASC, Id ASC
LIMIT :queryLimit
];
} else {
accountList = [
SELECT Id, Name
FROM Account
WHERE
Name LIKE :searchPattern
AND (Name > :lastAccountName
OR (Name = :lastAccountName AND Id > :lastAccountId))
WITH SECURITY_ENFORCED
ORDER BY Name ASC, Id ASC
LIMIT :queryLimit
];
}
Boolean hasMore = accountList.size() > ACCOUNT_PAGE_SIZE;
if (hasMore) {
accountList.remove(accountList.size() - 1);
}
// Apply FLS (Field Level Security) checks
SObjectAccessDecision securityDecision = Security.stripInaccessible(
AccessType.READABLE,
accountList
);
rt.isSuccess = true;
rt.message = '';
rt.dataList = securityDecision.getRecords();
rt.hasMore = hasMore;
} catch (Exception e) {
rt.isSuccess = false;
rt.message = 'アカウント取得エラー: ' + e.getMessage();
System.debug(
'ContactDataController.getAccountForLookup error: ' +
e.getStackTraceString()
);
}
return rt;
}
@AuraEnabled
public static ReturnValue getRecordType() {
ReturnValue rt = new ReturnValue();
try {
// Check object permission before reading the Contact record type metadata.
if (!Schema.SObjectType.Contact.isAccessible()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の参照権限がありません。'
);
}
// SOQL does not return the implicit Master record type when no custom
// Contact record types exist. Schema metadata includes all record types
// available to the current user, including Master.
List<RecordType> recordTypeList = new List<RecordType>();
for (
Schema.RecordTypeInfo recordTypeInfo : Schema.SObjectType.Contact.getRecordTypeInfos()
) {
if (recordTypeInfo.isActive() && recordTypeInfo.isAvailable()) {
recordTypeList.add(
new RecordType(
Id = recordTypeInfo.getRecordTypeId(),
Name = recordTypeInfo.getName()
)
);
}
}
rt.isSuccess = true;
rt.message = '';
rt.dataList = recordTypeList;
} catch (Exception e) {
rt.isSuccess = false;
rt.message = 'レコードタイプ取得エラー: ' + e.getMessage();
System.debug(
'ContactDataController.getRecordType error: ' + e.getStackTraceString()
);
}
return rt;
}
@AuraEnabled
public static ReturnValue getContactList() {
ReturnValue returnValue = new ReturnValue();
try {
// Check CRUD/FLS permissions before query
if (!Schema.SObjectType.Contact.isAccessible()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の参照権限がありません。'
);
}
List<Contact> contactList = [
SELECT
Id,
LastName,
FirstName,
HasOptedOutOfEmail,
Level__c,
LastCUUpdateDate
FROM Contact
WITH SECURITY_ENFORCED
ORDER BY LastName DESC
LIMIT 1000
];
// Apply FLS (Field Level Security) checks
SObjectAccessDecision securityDecision = Security.stripInaccessible(
AccessType.READABLE,
contactList
);
returnValue.isSuccess = true;
returnValue.message = '取得しました';
returnValue.dataList = securityDecision.getRecords();
} catch (Exception e) {
returnValue.isSuccess = false;
returnValue.message = '取引先責任者取得エラー: ' + e.getMessage();
System.debug(
'ContactDataController.getContactList error: ' + e.getStackTraceString()
);
}
return returnValue;
}
@AuraEnabled
public static ReturnValue searchContactList(String searchStr) {
ReturnValue rt = new ReturnValue();
try {
// Check CRUD/FLS permissions before query
if (!Schema.SObjectType.Contact.isAccessible()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の参照権限がありません。'
);
}
List<Contact> contactList;
if (String.isEmpty(searchStr) || searchStr == '*') {
contactList = [
SELECT LastName, FirstName, HasOptedOutOfEmail, Level__c
FROM Contact
WITH SECURITY_ENFORCED
ORDER BY LastName DESC
LIMIT 1000
];
} else {
String searchPattern = '%' + searchStr + '%';
contactList = [
SELECT Id, LastName, FirstName, HasOptedOutOfEmail, Level__c
FROM Contact
WHERE LastName LIKE :searchPattern
WITH SECURITY_ENFORCED
ORDER BY LastName DESC
LIMIT 1000
];
}
// Apply FLS (Field Level Security) checks
SObjectAccessDecision securityDecision = Security.stripInaccessible(
AccessType.READABLE,
contactList
);
rt.isSuccess = true;
rt.dataList = securityDecision.getRecords();
} catch (Exception e) {
rt.isSuccess = false;
rt.message = '検索エラー: ' + e.getMessage();
System.debug(
'ContactDataController.searchContactList error: ' +
e.getStackTraceString()
);
}
return rt;
}
@AuraEnabled
public static ReturnValue saveContacts(List<Contact> contactList) {
ReturnValue returnValue = new ReturnValue();
try {
// Check CRUD/FLS permissions before insert
if (!Schema.SObjectType.Contact.isCreateable()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の作成権限がありません。'
);
}
List<Contact> contactsToInsert = new List<Contact>();
for (Contact con : contactList) {
Contact newContact = con.clone(false, false, false, false);
newContact.AccountId = con.AccountId;
contactsToInsert.add(newContact);
}
// Apply FLS (Field Level Security) checks for create operation
SObjectAccessDecision securityDecision = Security.stripInaccessible(
AccessType.CREATABLE,
contactsToInsert
);
insert securityDecision.getRecords();
returnValue.isSuccess = true;
returnValue.dataList = securityDecision.getRecords();
returnValue.message =
securityDecision.getRecords().size() +
'件の取引先責任者を作成しました。';
} catch (DmlException e) {
returnValue.isSuccess = false;
returnValue.message = 'データ登録エラー: ' + e.getDmlMessage(0);
System.debug(
'ContactDataController.saveContacts DML error: ' + e.getMessage()
);
} catch (Exception e) {
returnValue.isSuccess = false;
returnValue.message = 'システムエラー: ' + e.getMessage();
System.debug(
'ContactDataController.saveContacts error: ' + e.getStackTraceString()
);
}
return returnValue;
}
@AuraEnabled
public static ReturnValue saveContactsForAccount(
Id accountId,
List<Contact> contactList
) {
ReturnValue returnValue = new ReturnValue();
try {
if (accountId == null) {
returnValue.isSuccess = false;
returnValue.message = '取引先を指定してください。';
return returnValue;
}
if (!Schema.SObjectType.Account.isAccessible()) {
throw new AuraHandledException(
'ユーザーに取引先の参照権限がありません。'
);
}
if (!Schema.SObjectType.Contact.isCreateable()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の作成権限がありません。'
);
}
if (!Schema.SObjectType.Contact.fields.AccountId.isCreateable()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の取引先項目を書き込む権限がありません。'
);
}
// with sharing plus SECURITY_ENFORCED ensures that the current user can
// access the target Account before any Contact is inserted.
Account targetAccount = [
SELECT Id
FROM Account
WHERE Id = :accountId
WITH SECURITY_ENFORCED
LIMIT 1
];
List<Contact> contactsToInsert = new List<Contact>();
for (Contact con : contactList) {
Contact newContact = con.clone(false, false, false, false);
// Never trust the AccountId supplied by the client payload.
newContact.AccountId = accountId;
contactsToInsert.add(newContact);
}
SObjectAccessDecision securityDecision = Security.stripInaccessible(
AccessType.CREATABLE,
contactsToInsert
);
insert securityDecision.getRecords();
returnValue.isSuccess = true;
returnValue.dataList = securityDecision.getRecords();
returnValue.message =
securityDecision.getRecords().size() +
'件の取引先責任者を作成しました。';
} catch (DmlException e) {
returnValue.isSuccess = false;
returnValue.message = 'データ登録エラー: ' + e.getDmlMessage(0);
System.debug(
'ContactDataController.saveContactsForAccount DML error: ' +
e.getMessage()
);
} catch (Exception e) {
returnValue.isSuccess = false;
returnValue.message = 'システムエラー: ' + e.getMessage();
System.debug(
'ContactDataController.saveContactsForAccount error: ' +
e.getStackTraceString()
);
}
return returnValue;
}
@AuraEnabled
public static ReturnValue deleteRow(Id id) {
ReturnValue rv = new ReturnValue();
try {
// Check CRUD/FLS permissions before query and delete
if (!Schema.SObjectType.Contact.isAccessible()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の参照権限がありません。'
);
}
if (!Schema.SObjectType.Contact.isDeletable()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の削除権限がありません。'
);
}
Contact contact = [
SELECT Id
FROM Contact
WHERE Id = :id
WITH SECURITY_ENFORCED
];
Database.delete(contact, false, AccessLevel.USER_MODE);
rv.isSuccess = true;
rv.message = '正常に削除しました。';
} catch (DmlException e) {
rv.isSuccess = false;
rv.message = '削除エラー: ' + e.getDmlMessage(0);
} catch (Exception e) {
rv.isSuccess = false;
rv.message = 'システムエラー: ' + e.getMessage();
}
return rv;
}
@AuraEnabled
public static ReturnValue updateContactRecord(Id id, Boolean flg) {
ReturnValue rv = new ReturnValue();
try {
// Check CRUD/FLS permissions before query and update
if (!Schema.SObjectType.Contact.isAccessible()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の参照権限がありません。'
);
}
if (!Schema.SObjectType.Contact.isUpdateable()) {
throw new AuraHandledException(
'ユーザーに取引先責任者の更新権限がありません。'
);
}
Contact contact = [
SELECT Id, HasOptedOutOfEmail
FROM Contact
WHERE Id = :id
WITH SECURITY_ENFORCED
];
contact.HasOptedOutOfEmail = !flg;
// Apply FLS (Field Level Security) checks for update operation
SObjectAccessDecision securityDecision = Security.stripInaccessible(
AccessType.UPDATABLE,
new List<Contact>{ contact }
);
update securityDecision.getRecords();
rv.isSuccess = true;
rv.message = '正常に更新しました。';
} catch (DmlException e) {
rv.isSuccess = false;
rv.message = '更新エラー: ' + e.getDmlMessage(0);
} catch (Exception e) {
rv.isSuccess = false;
rv.message = 'システムエラー: ' + e.getMessage();
}
return rv;
}
public class ReturnValue {
@AuraEnabled
public Boolean isSuccess { get; set; }
@AuraEnabled
public String message { get; set; }
@AuraEnabled
public List<Object> dataList { get; set; }
@AuraEnabled
public Boolean hasMore { get; set; }
}
}