Skip to content

Commit d8720b5

Browse files
committed
Add support for custom fields in CMDT
1 parent 00df166 commit d8720b5

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ trigger ExampleTrigger on Opportunity(after insert, after update, before delete)
3636

3737
That's it! Now you're ready to configure your rollups using Custom Metadata. `Rollup` makes heavy use of Entity Definition & Field Definition metadata fields, which allows you to simply select your options from within picklists, or dropdowns. This is great for giving you quick feedback on which objects/fields are available without requiring you to know the API name for every SObject and their corresponding field names.
3838

39+
#### Special Considerations For Use Of Custom Fields As Rollup/Lookup Fields
40+
41+
One **special** thing to note on the subject of Field Definitions — custom fields referenced in CMDT Field Definition fields are stored in an atypical way, and require the usage of additional SOQL queries as part of `Rollup`'s upfront cost. A typical `Rollup` operation will use `2` SOQL queries per rollup — the query that determines whether or not a job should be queued or batched, and a query for the specific Rollup Limit metadata (a dynamic query, which unfortunately means that it counts against the SOQL limits) — prior to going into the async context (where all limits are renewed) plus `1` SOQL qery (also dynamic, which is why it contributes even though it's querying CMDT). However, usage of custom fields as any of the four fields referenced in the `Rollup__mdt` custom metadata (details below) adds an additional SOQL query. If the SOQL queries used by `Rollup` becomes cause for concern, please submit an issue and we can work to address it!
42+
43+
#### Rollup Custom Metadata Field Breakdown
44+
3945
Within the `Rollup__mdt` custom metadata type, add a new record with fields:
4046

4147
- `Calc Item` - the SObject the calculation is derived from — in this case, Oppportunity

rollup/main/default/classes/Rollup.cls

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ global without sharing virtual class Rollup implements Database.Batchable<SObjec
10101010
return batchRollup;
10111011
}
10121012

1013+
private static Map<String, String> durableIdToFieldName;
10131014
private static SObjectField getSObjectFieldByName(DescribeSObjectResult objectDescribe, String desiredField) {
10141015
Map<String, SObjectField> fieldNameToField = objectDescribe.fields.getMap();
10151016
String paredFieldName = getParedFieldName(desiredField, objectDescribe);
@@ -1019,9 +1020,23 @@ global without sharing virtual class Rollup implements Database.Batchable<SObjec
10191020
// for lookup fields, CMDT field-level definition fields store the field name, which is outrageous
10201021
else if(fieldNameToField.containsKey(paredFieldName + 'Id')) {
10211022
return fieldNameToField.get(paredFieldName + 'Id');
1022-
} else if(fieldNameToField.containsKey(paredFieldName + '__c')) {
1023-
return fieldNameToField.get(paredFieldName + '__c');
10241023
}
1024+
1025+
try {
1026+
Id testCustomField = Id.valueOf(paredFieldName);
1027+
if(durableIdToFieldName == null) {
1028+
durableIdToFieldName = new Map<String, String>();
1029+
List<FieldDefinition> fieldDefinitions = [SELECT QualifiedApiName, DurableId FROM FieldDefinition WHERE EntityDefinitionId = : objectDescribe.getName()];
1030+
for(FieldDefinition fieldDef : fieldDefinitions) {
1031+
durableIdToFieldName.put(fieldDef.DurableId, fieldDef.QualifiedApiName);
1032+
}
1033+
}
1034+
String actualFieldName = durableIdToFieldName.get(desiredField);
1035+
return fieldNameToField.get(actualFieldName);
1036+
} catch(Exception ex) {
1037+
// do nothing, it didn't work
1038+
}
1039+
10251040
return null;
10261041
}
10271042

0 commit comments

Comments
 (0)