-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomMultiSelectLookupController.cls
More file actions
90 lines (79 loc) · 4.87 KB
/
CustomMultiSelectLookupController.cls
File metadata and controls
90 lines (79 loc) · 4.87 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
public with sharing class CustomMultiSelectLookupController {
@AuraEnabled(cacheable=true)
public static List<sObject> getLookupData( String searchKey,
String objectApiName,
String fieldApiName,
Integer maxQueryResult,
String selectCriteria,
List<RecordType> recordTypes){
System.debug('searchKey: ' + searchKey);
System.debug('objectApiName: ' + objectApiName);
System.debug('fieldApiName: ' + fieldApiName);
System.debug('maxQueryResult: ' + maxQueryResult);
System.debug('selectCriteria: ' + selectCriteria);
for(RecordType rt : recordTypes) {
System.debug('RecordTypeId: ' + rt.Id + ' DeveloperName: ' + rt.DeveloperName);
}
// local variables
List<sObject> sObjectList = new List<sObject>();
List<String> recordTypeIds = new List<String>();
String searchQuery;
String selectionCriteria;
try {
// Set default if maxQueryResult was not sent or set to 0
Integer maxLimit = (maxQueryResult < 1) ? 20 : maxQueryResult;
// When there is no search key or record types, return an empty list
if(String.isBlank(searchKey) && recordTypes.isEmpty() && String.isBlank(selectCriteria)) {
return sObjectList;
} else {
// Verify Object & Search Field Names
Schema.SObjectType sObjectType = Schema.getGlobalDescribe().get(objectApiName);
if (sObjectType == null) {
throw new AuraHandledException('objectApiName ' + objectApiName + ' is not valid');
} else {
// Get the SObject description and field map
Schema.DescribeSObjectResult describeResult = sObjectType.getDescribe();
Map<String, Schema.SObjectField> fieldMap = describeResult.fields.getMap();
if (fieldMap.containsKey(fieldApiName)) {
// Build search variable
String key = '%' + String.escapeSingleQuotes(searchKey) + '%';
//Set Selection Criteria
if (String.isBlank(selectCriteria)){
selectionCriteria = '';
} else {
selectionCriteria = selectCriteria;
}
// Build queries based on other inputs: Record Types and Search Field value
if (recordTypes.isEmpty()) {
if (String.isBlank(searchKey)){
String criteriaSubString = selectionCriteria.removeStartIgnoreCase('AND');
searchQuery = 'SELECT Id, ' + fieldApiName + ' FROM ' + objectApiName + ' WHERE ' + criteriaSubString + ' ORDER BY ' + fieldApiName + ' LIMIT ' + maxLimit;
} else {
searchQuery = 'SELECT Id, ' + fieldApiName + ' FROM ' + objectApiName + ' WHERE ' + fieldApiName + ' LIKE :key ' + selectionCriteria + ' ORDER BY ' + fieldApiName + ' LIMIT ' + maxLimit;
}
} else {
// Parametize Record Type Ids for query
for (RecordType recordType : recordTypes) {
recordTypeIds.add(recordType.Id);
}
// Include Record Type selection with or without Search Field value
if (String.isBlank(searchKey)) {
searchQuery = 'SELECT Id, ' + fieldApiName + ' FROM ' + objectApiName + ' WHERE RecordTypeId IN :recordTypeIds ' + selectionCriteria + ' ORDER BY ' + fieldApiName + ' LIMIT ' + maxLimit;
} else {
searchQuery = 'SELECT Id, ' + fieldApiName + ' FROM ' + objectApiName + ' WHERE RecordTypeId IN :recordTypeIds AND ' + fieldApiName + ' LIKE :key ' + selectionCriteria + ' ORDER BY ' + fieldApiName + ' LIMIT ' + maxLimit;
}
}
// Execute query
System.debug('searchQuery: ' + searchQuery);
sObjectList = Database.query(searchQuery, AccessLevel.USER_MODE);
return SObjectList;
} else {
throw new AuraHandledException('Object Api Name and Field Api Name must be valid.');
}
}
}
} catch (Exception e) {
throw new AuraHandledException(e.getMessage());
}
}
}