-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathQuery.cls
More file actions
623 lines (519 loc) · 22.8 KB
/
Query.cls
File metadata and controls
623 lines (519 loc) · 22.8 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
/*******************************************************************************************************
* This file is part of the Nebula Query & Search project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaQueryAndSearch for full license details. *
******************************************************************************************************/
/**
* @group SOQL Queries
* @description Handles generating & executing SObject queries
* @see SOQL
* @see AggregateQuery
*/
@SuppressWarnings(
'PMD.ApexDoc, PMD.AvoidGlobalModifier, PMD.CognitiveComplexity, PMD.CyclomaticComplexity, PMD.EagerlyLoadedDescribeSObjectResult, PMD.ExcessivePublicCount, PMD.StdCyclomaticComplexity'
)
global class Query extends SOQL {
private String displayFieldApiName;
private List<String> polymorphicFieldStatements;
private List<String> childRelationshipQueries;
private Boolean forReference;
private Boolean forUpdate;
private Boolean forView;
private Boolean includeLabels;
private Boolean includeFormattedValues;
global Query(Schema.SObjectType sobjectType) {
super(sobjectType, true);
this.displayFieldApiName = this.getDisplayFieldApiName(this.sobjectType);
this.polymorphicFieldStatements = new List<String>();
this.childRelationshipQueries = new List<String>();
this.forReference = false;
this.forUpdate = false;
this.forView = false;
this.includeLabels = false;
this.includeFormattedValues = false;
this.addDefaultFields();
}
global Query addField(Schema.SObjectField field) {
return this.addField(field, null);
}
global Query addField(Schema.SObjectField field, SOQL.FieldCategory fieldCategory) {
return this.addFields(new List<Schema.SObjectField>{ field }, fieldCategory);
}
global Query addField(SOQL.QueryField queryField) {
return this.addField(queryField, null);
}
global Query addField(SOQL.QueryField queryField, SOQL.FieldCategory fieldCategory) {
return this.addFields(new List<SOQL.QueryField>{ queryField }, fieldCategory);
}
global Query addFields(List<Schema.SObjectField> fields) {
return this.addFields(fields, null);
}
global Query addFields(List<Schema.SObjectField> fields, SOQL.FieldCategory fieldCategory) {
List<SOQL.QueryField> queryFields = new List<SOQL.QueryField>();
for (Schema.SObjectField field : fields) {
queryFields.add(new SOQL.QueryField(field));
}
return this.addFields(queryFields, fieldCategory);
}
global Query addFields(List<SOQL.QueryField> queryFields) {
return this.addFields(queryFields, null);
}
global Query addFields(SOQL.FieldCategory fieldCategory) {
List<SOQL.QueryField> queryFields = new List<SOQL.QueryField>();
for (Schema.SObjectField field : this.sobjectDescribe.fields.getMap().values()) {
queryFields.add(new SOQL.QueryField(field));
}
return this.addFields(queryFields, fieldCategory);
}
global Query addFields(List<SOQL.QueryField> queryFields, SOQL.FieldCategory fieldCategory) {
super.doAddFields(queryFields, fieldCategory);
return this.setHasChanged();
}
global Query addFieldSet(Schema.FieldSet fieldSet) {
return this.addFieldSet(fieldSet, null);
}
global Query addFieldSet(Schema.FieldSet fieldSet, SOQL.FieldCategory fieldCategory) {
List<SOQL.QueryField> queryFields = new List<SOQL.QueryField>();
for (Schema.FieldSetMember fieldSetMember : fieldSet.getFields()) {
queryFields.add(new SOQL.QueryField(this.sobjectType, fieldSetMember.getFieldPath()));
}
return this.addFields(queryFields, fieldCategory);
}
global Query addPolymorphicFields(Schema.SObjectField polymorphicRelationshipField) {
return addPolymorphicFields(polymorphicRelationshipField, new Map<Schema.SObjectType, List<Schema.SObjectField>>());
}
global Query addPolymorphicFields(Schema.SObjectField polymorphicRelationshipField, Map<Schema.SObjectType, List<Schema.SObjectField>> fieldsBySObjectType) {
Map<Schema.SObjectType, List<SOQL.QueryField>> queryFieldsBySObjectType = new Map<Schema.SObjectType, List<SOQL.QueryField>>();
for (Schema.SObjectType sobjectType : fieldsBySObjectType.keySet()) {
List<SOQL.QueryField> queryFields = new List<SOQL.QueryField>();
for (Schema.SObjectField field : fieldsBySObjectType.get(sobjectType)) {
queryFields.add(new SOQL.QueryField(field));
}
queryFieldsBySObjectType.put(sobjectType, queryFields);
}
return this.addPolymorphicFields(polymorphicRelationshipField, queryFieldsBySObjectType);
}
@SuppressWarnings('PMD.StdCyclomaticComplexity')
global Query addPolymorphicFields(Schema.SObjectField polymorphicRelationshipField, Map<Schema.SObjectType, List<SOQL.QueryField>> queryFieldsBySObjectType) {
String polymorphicFieldStatement = queryFieldsBySObjectType.isEmpty() ? '' : 'TYPEOF ' + polymorphicRelationshipField.getDescribe().getRelationshipName();
for (Schema.SObjectType sobjectType : queryFieldsBySObjectType.keySet()) {
List<String> fieldNames = new List<String>();
for (SOQL.QueryField queryField : queryFieldsBySObjectType.get(sobjectType)) {
fieldNames.addAll(this.getFieldsToQuery(queryField, SOQL.FieldCategory.ACCESSIBLE));
}
fieldNames.sort();
polymorphicFieldStatement += ' WHEN ' + sobjectType + ' THEN ' + String.join(fieldNames, ', ');
}
// The Name object contains the list of all possible polymorphic fields in the org
List<String> supportedPolymorphicFieldNames = new List<String>();
for (Schema.SObjectField field : Schema.Name.SObjectType.getDescribe(Schema.SObjectDescribeOptions.DEFERRED).fields.getMap().values()) {
supportedPolymorphicFieldNames.addAll(this.getFieldsToQuery(new QueryField(field), SOQL.FieldCategory.ACCESSIBLE));
}
supportedPolymorphicFieldNames.sort();
if (!queryFieldsBySObjectType.isEmpty()) {
polymorphicFieldStatement += ' ELSE ';
} else if (queryFieldsBySObjectType.isEmpty()) {
String supportedPolymorphicFieldPrefix = queryFieldsBySObjectType.isEmpty() ? 'Who.' : '';
for (Integer i = 0; i < supportedPolymorphicFieldNames.size(); i++) {
supportedPolymorphicFieldNames[i] = supportedPolymorphicFieldPrefix + supportedPolymorphicFieldNames[i];
}
}
polymorphicFieldStatement += String.join(supportedPolymorphicFieldNames, ', ');
if (!queryFieldsBySObjectType.isEmpty()) {
polymorphicFieldStatement += ' END';
}
this.polymorphicFieldStatements.add(polymorphicFieldStatement);
return this.setHasChanged();
}
global Query includeLabels() {
this.includeLabels = true;
return this.setHasChanged();
}
global Query includeFormattedValues() {
this.includeFormattedValues = true;
return this.setHasChanged();
}
global Query removeField(Schema.SObjectField field) {
return this.removeFields(new List<Schema.SObjectField>{ field });
}
global Query removeField(SOQL.QueryField queryField) {
return this.removeFields(new List<SOQL.QueryField>{ queryField });
}
global Query removeFields(Schema.FieldSet fieldSet) {
List<SOQL.QueryField> queryFields = new List<SOQL.QueryField>();
for (Schema.FieldSetMember fieldSetMember : fieldSet.getFields()) {
queryFields.add(new SOQL.QueryField(this.getSObjectType(), fieldSetMember.getFieldPath()));
}
return this.removeFields(queryFields);
}
global Query removeFields(List<Schema.SObjectField> fields) {
List<SOQL.QueryField> queryFields = new List<SOQL.QueryField>();
for (Schema.SObjectField field : fields) {
queryFields.add(new SOQL.QueryField(field));
}
return this.removeFields(queryFields);
}
global Query removeFields(List<SOQL.QueryField> queryFields) {
super.doRemoveFields(queryFields);
return this.setHasChanged();
}
global Query includeRelatedRecords(Schema.SObjectField childToParentRelationshipField, Query relatedSObjectQuery) {
this.childRelationshipQueries.add(relatedSObjectQuery.getRelatedRecordsQuery(childToParentRelationshipField));
return this.setHasChanged();
}
global Query usingScope(Scope scope) {
super.doUsingScope(scope);
return this.setHasChanged();
}
global Query filterWhere(Schema.SObjectField field, SOQL.Operator operator, Object value) {
return this.filterWhere(new SOQL.QueryField(field), operator, value);
}
global Query filterWhere(Schema.SObjectField field, SOQL.Operator operator, Object value, String bindWithKey) {
return this.filterWhere(new SOQL.QueryField(field), operator, value, bindWithKey);
}
global Query filterWhere(SOQL.QueryField queryField, SOQL.Operator operator, Object value) {
return this.filterWhere(new SOQL.QueryFilter(queryField, operator, value));
}
global Query filterWhere(SOQL.QueryField queryField, SOQL.Operator operator, Object value, String bindWithKey) {
return this.filterWhere(new SOQL.QueryFilter(queryField, operator, value, bindWithKey));
}
global Query filterWhere(SOQL.QueryFilter filter) {
return this.filterWhere(new List<SOQL.QueryFilter>{ filter });
}
global Query filterWhere(List<SOQL.QueryFilter> filters) {
super.doFilterWhere(filters);
return this.setHasChanged();
}
global Query orFilterWhere(List<SOQL.QueryFilter> filters) {
super.doOrFilterWhere(filters);
return this.setHasChanged();
}
global Query setWhereFilterLogic(String filterLogic) {
super.doSetWhereFilterLogic(filterLogic);
return this.setHasChanged();
}
//global Query filterWhereInSubquery(Schema.SObjectType childSObjectType, Schema.SObjectField lookupFieldOnChildSObject) {
//this.whereFilters.add('Id IN (SELECT ' + lookupFieldOnChildSObject + ' FROM ' + childSObjectType + ')');
//return this.setHasChanged();
//}
//
//global Query filterWhereInSubquery(Query childQuery, Schema.SObjectField lookupFieldOnChildSObject) {
//String subqueryString = childQuery.getSubquery(lookupFieldOnChildSObject);
//this.whereFilters.add('Id IN ' + subqueryString);
//return this.setHasChanged();
//}
//
//global Query filterWhereNotInSubquery(Schema.SObjectType childSObjectType, Schema.SObjectField lookupFieldOnChildSObject) {
//this.whereFilters.add('Id NOT IN (SELECT ' + lookupFieldOnChildSObject + ' FROM ' + childSObjectType + ')');
//return this.setHasChanged();
//}
//
//global Query filterWhereNotInSubquery(Query childQuery, Schema.SObjectField lookupFieldOnChildSObject) {
//String subqueryString = childQuery.getSubquery(lookupFieldOnChildSObject);
//this.whereFilters.add('Id NOT IN ' + subqueryString);
//return this.setHasChanged();
//}
global Query withAccessLevel(System.AccessLevel accessLevel) {
super.doWithAccessLevel(accessLevel);
return this.setHasChanged();
}
global Query orderByField(Schema.SObjectField field) {
return this.orderByField(new SOQL.QueryField(field));
}
global Query orderByField(SOQL.QueryField queryField) {
return this.orderByField(queryField, null);
}
global Query orderByField(Schema.SObjectField field, SOQL.SortOrder sortOrder) {
return this.orderByField(field, sortOrder, null);
}
global Query orderByField(SOQL.QueryField queryField, SOQL.SortOrder sortOrder) {
return this.orderByField(queryField, sortOrder, null);
}
global Query orderByField(Schema.SObjectField field, SOQL.SortOrder sortOrder, Boolean sortNullsFirst) {
return this.orderByField(new SOQL.QueryField(field), sortOrder, sortNullsFirst);
}
global Query orderByField(SOQL.QueryField queryField, SOQL.SortOrder sortOrder, Boolean sortNullsFirst) {
super.doOrderBy(queryField, sortOrder, sortNullsFirst);
return this.setHasChanged();
}
global Query limitTo(Integer numberOfRecords) {
super.doLimitTo(numberOfRecords);
return this.setHasChanged();
}
global Query offsetBy(Integer offset) {
super.doOffsetBy(offset);
return this.setHasChanged();
}
global Query forReference() {
this.forReference = true;
return this.setHasChanged();
}
global Query forUpdate() {
this.forUpdate = true;
return this.setHasChanged();
}
global Query forView() {
this.forView = true;
return this.setHasChanged();
}
global Query setBind(String key, Object value) {
super.doSetBind(key, value);
return this.setHasChanged();
}
global Query setBinds(Map<String, Object> binds) {
super.doSetBinds(binds);
return this.setHasChanged();
}
global Query removeBind(String key) {
super.doRemoveBind(key);
return this.setHasChanged();
}
global Query clearBinds() {
super.doClearBinds();
return this.setHasChanged();
}
global Query generateBindVariableKeys() {
super.doGenerateBindVariableKeys();
return this;
}
// TODO decide if this should be global
public Query cacheResults() {
super.doCacheResults();
return this;
}
// TODO decide if this should be global
@SuppressWarnings('PMD.AvoidDebugStatements')
public override String getQuery() {
if (this.query != null && !this.hasChanged) {
return this.query;
}
String queryFieldString = this.getQueryFieldString();
String polymorphicFieldsString = String.join(this.polymorphicFieldStatements, ', ');
String polymorphicFieldsDelimiter = !String.isEmpty(queryFieldString) && !String.isEmpty(polymorphicFieldsString) ? ', ' : '';
String childRelationshipsQueryFieldString = this.getChildRelationshipsQueryFieldString();
String childRelationshipDelimiter = !String.isEmpty(queryFieldString) && !String.isEmpty(childRelationshipsQueryFieldString) ? ', ' : '';
this.query =
'SELECT ' +
queryFieldString +
polymorphicFieldsDelimiter +
polymorphicFieldsString +
childRelationshipDelimiter +
childRelationshipsQueryFieldString +
' FROM ' +
this.sobjectType +
super.doGetUsingScopeString() +
super.doGetWhereClauseString() +
super.doGetOrderByString() +
super.doGetLimitCountString() +
super.doGetOffetString() +
this.getForReferenceString() +
this.getForUpdateString() +
this.getForViewString();
// Change hasChanged to false so that subsequent calls to getQuery() use the cached query string
// If additional builder methods are later called, the builder methods will set hasChanged = true
this.hasChanged = false;
System.debug(System.LoggingLevel.FINEST, this.query);
return this.query;
}
@SuppressWarnings('PMD.AvoidDebugStatements')
public String getRelatedRecordsQuery(Schema.SObjectField childToParentRelationshipField) {
Schema.SObjectType parentSObjectType = childToParentRelationshipField.getDescribe().getReferenceTo()[0];
// Get the relationship name
String childRelationshipName;
for (Schema.ChildRelationship childRelationship : parentSObjectType.getDescribe(Schema.SObjectDescribeOptions.FULL).getChildRelationships()) {
if (childRelationship.getField() != childToParentRelationshipField) {
continue;
}
childRelationshipName = childRelationship.getRelationshipName();
}
String childQuery =
'(SELECT ' +
super.doGetQueryFieldString() +
' FROM ' +
childRelationshipName +
super.doGetUsingScopeString() +
super.doGetWhereClauseString() +
super.doGetOrderByString() +
super.doGetLimitCountString() +
')';
System.debug(System.LoggingLevel.FINEST, childQuery);
return childQuery;
}
@SuppressWarnings('PMD.AvoidDebugStatements')
public String getSubquery(Schema.SObjectField childToParentRelationshipField) {
String subquery =
'(SELECT ' +
childToParentRelationshipField +
' FROM ' +
this.sobjectType +
super.doGetUsingScopeString() +
super.doGetWhereClauseString() +
super.doGetOrderByString() +
super.doGetLimitCountString() +
')';
System.debug(System.LoggingLevel.FINEST, subquery);
return subquery;
}
@SuppressWarnings('PMD.AvoidDebugStatements')
public String getSearchQuery() {
String sobjectTypeOptions = super.doGetQueryFieldString() + super.doGetWhereClauseString() + super.doGetOrderByString() + super.doGetLimitCountString();
// If we have any sobject-specific options, then wrap the options in parentheses
sobjectTypeOptions = String.isEmpty(sobjectTypeOptions) ? '' : '(' + sobjectTypeOptions + ')';
String searchQuery = this.getSObjectType() + sobjectTypeOptions;
System.debug(System.LoggingLevel.FINEST, searchQuery);
return searchQuery;
}
global SObject getFirstResult() {
return super.doGetFirstResult();
}
global List<SObject> getResults() {
return super.doGetResults();
}
private void addDefaultFields() {
Map<String, Schema.SObjectField> fieldMap = this.getSObjectType().getDescribe(Schema.SObjectDescribeOptions.DEFERRED).fields.getMap();
this.addField(fieldMap.get('Id'));
if (!String.isBlank(this.displayFieldApiName)) {
this.addField(fieldMap.get(this.displayFieldApiName));
}
}
private Query setHasChanged() {
this.doSetHasChanged();
return this;
}
private String getQueryFieldString() {
Set<String> distinctFieldApiNamesToQuery = new Set<String>();
for (SOQL.QueryField queryField : this.includedQueryFieldsAndCategory.keySet()) {
SOQL.FieldCategory fieldCategory = this.includedQueryFieldsAndCategory.get(queryField);
List<String> fieldsToQuery = this.getFieldsToQuery(queryField, fieldCategory);
if (!fieldsToQuery.isEmpty()) {
distinctFieldApiNamesToQuery.addAll(fieldsToQuery);
}
}
// Remove an excluded field paths
for (SOQL.QueryField excludedQueryField : this.excludedQueryFields) {
distinctFieldApiNamesToQuery.remove(excludedQueryField.toString());
}
List<String> fieldApiNamesToQuery = new List<String>(distinctFieldApiNamesToQuery);
fieldApiNamesToQuery.sort();
return String.join(fieldApiNamesToQuery, ', ');
}
private String getDisplayFieldApiName(Schema.SObjectType sobjectType) {
// There are several commonly used names for the display field name - typically, Name
// The order of the field names has been sorted based on number of objects in a new dev org with that field
List<String> possibleDisplayFieldApiNames = new List<String>{
'Name',
'DeveloperName',
'ApiName',
'Title',
'Subject',
'AssetRelationshipNumber',
'CaseNumber',
'ContractNumber',
'Domain',
'FriendlyName',
'FunctionName',
'Label',
'LocalPart',
'OrderItemNumber',
'OrderNumber',
'SolutionName',
'TestSuiteName'
};
Map<String, Schema.SObjectField> fieldMap = sobjectType.getDescribe(Schema.SObjectDescribeOptions.DEFERRED).fields.getMap();
for (String fieldApiName : possibleDisplayFieldApiNames) {
Schema.SObjectField field = fieldMap.get(fieldApiName);
if (field == null) {
continue;
}
Schema.DescribeFieldResult fieldDescribe = field.getDescribe();
if (fieldDescribe.isNameField()) {
return fieldDescribe.getName();
}
}
return null;
}
private String getParentSObjectNameField(Schema.DescribeFieldResult fieldDescribe) {
String relationshipName = fieldDescribe.getRelationshipName();
Schema.SObjectType parentSObjectType = fieldDescribe.getReferenceTo()[0];
String nameField = this.getDisplayFieldApiName(parentSObjectType);
if (relationshipName == null) {
return null;
} else if (nameField == null) {
return null;
} else {
return relationshipName + '.' + nameField;
}
}
@SuppressWarnings('PMD.AvoidDeeplyNestedIfStmts, PMD.NcssMethodCount')
private List<String> getFieldsToQuery(SOQL.QueryField queryField, SOQL.FieldCategory fieldCat) {
List<String> fieldsToReturn = new List<String>();
if (fieldCat == null) {
return fieldsToReturn;
} else if (fieldCat == SOQL.FieldCategory.ACCESSIBLE && !queryField.getDescribe().isAccessible()) {
return fieldsToReturn;
} else if (fieldCat == SOQL.FieldCategory.UPDATEABLE && !queryField.getDescribe().isUpdateable()) {
return fieldsToReturn;
} else if (fieldCat == SOQL.FieldCategory.STANDARD && queryField.getDescribe().isCustom()) {
return fieldsToReturn;
} else if (fieldCat == SOQL.FieldCategory.CUSTOM && !queryField.getDescribe().isCustom()) {
return fieldsToReturn;
}
fieldsToReturn.add(queryField.toString());
// If the field has picklist options, then it can be translated
if (this.includeLabels && !queryField.getDescribe().getPickListValues().isEmpty()) {
fieldsToReturn.add(this.getFieldToLabel(queryField.getDescribe().getName()));
}
// If the field is a number, date, time, or currency, it can be formatted
List<Schema.DisplayType> supportedTypesForFormatting = new List<Schema.DisplayType>{
Schema.DisplayType.CURRENCY,
Schema.DisplayType.DATE,
Schema.DisplayType.DATETIME,
Schema.DisplayType.DOUBLE,
Schema.DisplayType.INTEGER,
Schema.DisplayType.PERCENT,
Schema.DisplayType.TIME
};
if (this.includeFormattedValues && supportedTypesForFormatting.contains(queryField.getDescribe().getType())) {
fieldsToReturn.add(this.getFieldFormattedValue(queryField.getDescribe().getName()));
}
// If the field is a lookup, then we need to get the name field from the parent object
if (queryField.getDescribe().getType().name() == 'REFERENCE') {
if (queryField.getDescribe().isNamePointing()) {
String fieldPath = queryField.getFieldPath();
Integer indx = fieldPath.lastIndexOf(queryField.getDescribe().getName());
String parentTypeFieldPath = fieldPath.substring(0, indx) + queryField.getDescribe().getRelationshipName() + '.Type';
fieldsToReturn.add(parentTypeFieldPath);
}
String parentNameField = this.getParentSObjectNameField(queryField.getDescribe());
if (parentNameField != null) {
fieldsToReturn.add(parentNameField);
// Record type names can be translated, so include the translation
if (this.includeLabels && queryField.toString() == 'RecordTypeId') {
fieldsToReturn.add(this.getFieldToLabel(parentNameField));
}
}
}
return fieldsToReturn;
}
private String getChildRelationshipsQueryFieldString() {
if (this.childRelationshipQueries.isEmpty()) {
return '';
}
this.childRelationshipQueries.sort();
return String.join(this.childRelationshipQueries, ', ');
}
private String getFieldToLabel(String fieldApiName) {
return 'toLabel(' + fieldApiName + ') ' + fieldApiName.replace('.', '_') + '__Label';
}
private String getFieldFormattedValue(String fieldApiName) {
return 'format(' + fieldApiName + ') ' + fieldApiName.replace('.', '_') + '__Formatted';
}
private String getForReferenceString() {
return !this.forReference ? '' : ' FOR REFERENCE';
}
private String getForUpdateString() {
return !this.forUpdate ? '' : ' FOR UPDATE';
}
private String getForViewString() {
return !this.forView ? '' : ' FOR VIEW';
}
}