@@ -177,6 +177,10 @@ public virtual inherited sharing class SOQL implements Queryable {
177177 Set <String > toValuesOf (String relationshipName , SObjectField targetKeyField );
178178 // ID MAP
179179 Map <Id , SObject > toMap ();
180+ Map <Id , SObject > toIdMapBy (SObjectField field );
181+ Map <Id , SObject > toIdMapBy (String relationshipName , SObjectField targetKeyField );
182+ Map <Id , List <SObject >> toAggregatedIdMapBy (SObjectField keyField );
183+ Map <Id , List <SObject >> toAggregatedIdMapBy (String relationshipName , SObjectField targetKeyField );
180184 // VALUE MAP
181185 Map <String , SObject > toMap (SObjectField keyField );
182186 Map <String , SObject > toMap (String relationshipName , SObjectField targetKeyField );
@@ -1018,42 +1022,73 @@ public virtual inherited sharing class SOQL implements Queryable {
10181022 return this .converter .transform (this .executor .toList ()).toMap ();
10191023 }
10201024
1025+ public Map <Id , SObject > toIdMapBy (SObjectField field ) {
1026+ this .with (field );
1027+ this .whereAre (Filter .with (field ).isNotNull ());
1028+ return this .converter .transform (this .executor .toList ()).toIdMapBy (field );
1029+ }
1030+
1031+ public Map <Id , SObject > toIdMapBy (String relationshipName , SObjectField targetKeyField ) {
1032+ this .with (relationshipName + ' .' + targetKeyField .toString ());
1033+ this .whereAre (Filter .with (relationshipName , targetKeyField ).isNotNull ());
1034+ return this .converter .transform (this .executor .toList ()).toIdMapBy (relationshipName , targetKeyField );
1035+ }
1036+
1037+ public Map <Id , List <SObject >> toAggregatedIdMapBy (SObjectField keyField ) {
1038+ this .with (keyField );
1039+ this .whereAre (Filter .with (keyField ).isNotNull ());
1040+ return this .converter .transform (this .executor .toList ()).toAggregatedIdMapBy (keyField );
1041+ }
1042+
1043+ public Map <Id , List <SObject >> toAggregatedIdMapBy (String relationshipName , SObjectField targetKeyField ) {
1044+ this .with (relationshipName + ' .' + targetKeyField .toString ());
1045+ this .whereAre (Filter .with (relationshipName , targetKeyField ).isNotNull ());
1046+ return this .converter .transform (this .executor .toList ()).toAggregatedIdMapBy (relationshipName , targetKeyField );
1047+ }
1048+
10211049 public Map <String , SObject > toMap (SObjectField keyField ) {
10221050 this .with (keyField );
1051+ this .whereAre (Filter .with (keyField ).isNotNull ());
10231052 return this .converter .transform (this .executor .toList ()).toMap (keyField );
10241053 }
10251054
10261055 public Map <String , SObject > toMap (String relationshipName , SObjectField targetKeyField ) {
10271056 this .with (relationshipName + ' .' + targetKeyField .toString ());
1057+ this .whereAre (Filter .with (relationshipName , targetKeyField ).isNotNull ());
10281058 return this .converter .transform (this .executor .toList ()).toMap (relationshipName , targetKeyField );
10291059 }
10301060
10311061 public Map <String , String > toMap (SObjectField keyField , SObjectField valueField ) {
10321062 this .builder .fields .clearAllFields (); // other fields not needed
10331063 this .with (keyField , valueField );
1064+ this .whereAre (Filter .with (keyField ).isNotNull ());
10341065 return this .converter .transform (this .executor .toList ()).toMap (keyField , valueField );
10351066 }
10361067
10371068 public Map <String , List <SObject >> toAggregatedMap (SObjectField keyField ) {
10381069 this .with (keyField );
1070+ this .whereAre (Filter .with (keyField ).isNotNull ());
10391071 return this .converter .transform (this .executor .toList ()).toAggregatedMap (keyField );
10401072 }
10411073
10421074 public Map <String , List <SObject >> toAggregatedMap (String relationshipName , SObjectField targetKeyField ) {
10431075 this .with (relationshipName + ' .' + targetKeyField .toString ());
1076+ this .whereAre (Filter .with (relationshipName , targetKeyField ).isNotNull ());
10441077 return this .converter .transform (this .executor .toList ()).toAggregatedMap (relationshipName , targetKeyField );
10451078 }
10461079
10471080 public Map <String , List <String >> toAggregatedMap (SObjectField keyField , SObjectField valueField ) {
10481081 this .builder .fields .clearAllFields (); // other fields not needed
10491082 this .with (keyField , valueField );
1083+ this .whereAre (Filter .with (keyField ).isNotNull ());
10501084 return this .converter .transform (this .executor .toList ()).toAggregatedMap (keyField , valueField );
10511085 }
10521086
10531087 public Map <String , List <String >> toAggregatedMap (SObjectField keyField , String relationshipName , SObjectField targetKeyField ) {
10541088 this .builder .fields .clearAllFields (); // other fields not needed
10551089 this .with (keyField );
10561090 this .with (relationshipName + ' .' + targetKeyField .toString ());
1091+ this .whereAre (Filter .with (keyField ).isNotNull ());
10571092 return this .converter .transform (this .executor .toList ()).toAggregatedMap (keyField , relationshipName , targetKeyField );
10581093 }
10591094
@@ -2726,6 +2761,60 @@ public virtual inherited sharing class SOQL implements Queryable {
27262761 return recordPerId ;
27272762 }
27282763
2764+ public Map <Id , SObject > toIdMapBy (SObjectField field ) {
2765+ Map <Id , SObject > recordPerCustomKey = (Map <Id , SObject >) Type .forName (' Map<Id, ' + this .ofObject + ' >' ).newInstance ();
2766+
2767+ for (SObject record : this .recordsToTransform ) {
2768+ recordPerCustomKey .put ((Id ) record .get (field ), record );
2769+ }
2770+
2771+ return recordPerCustomKey ;
2772+ }
2773+
2774+ public Map <Id , SObject > toIdMapBy (String relationshipName , SObjectField targetKeyField ) {
2775+ Map <Id , SObject > recordPerCustomKey = (Map <Id , SObject >) Type .forName (' Map<Id, ' + this .ofObject + ' >' ).newInstance ();
2776+ List <String > relationshipPathFields = relationshipName .split (' \\ .' );
2777+
2778+ for (SObject record : this .recordsToTransform ) {
2779+ recordPerCustomKey .put ((Id ) extractNestedFieldValue (record , relationshipPathFields , targetKeyField ), record );
2780+ }
2781+
2782+ return recordPerCustomKey ;
2783+ }
2784+
2785+ public Map <Id , List <SObject >> toAggregatedIdMapBy (SObjectField keyField ) {
2786+ Map <Id , List <SObject >> recordsPerCustomKey = (Map <Id , List <SObject >>) Type .forName (' Map<Id, List<' + this .ofObject + ' >>' ).newInstance ();
2787+
2788+ for (SObject record : this .recordsToTransform ) {
2789+ Id key = (Id ) record .get (keyField );
2790+
2791+ if (! recordsPerCustomKey .containsKey (key )) {
2792+ recordsPerCustomKey .put (key , new List <SObject >());
2793+ }
2794+
2795+ recordsPerCustomKey .get (key ).add (record );
2796+ }
2797+
2798+ return recordsPerCustomKey ;
2799+ }
2800+
2801+ public Map <Id , List <SObject >> toAggregatedIdMapBy (String relationshipName , SObjectField targetKeyField ) {
2802+ Map <Id , List <SObject >> recordsPerCustomKey = (Map <Id , List <SObject >>) Type .forName (' Map<Id, List<' + this .ofObject + ' >>' ).newInstance ();
2803+ List <String > relationshipPathFields = relationshipName .split (' \\ .' );
2804+
2805+ for (SObject record : this .recordsToTransform ) {
2806+ Id key = extractNestedFieldValue (record , relationshipPathFields , targetKeyField );
2807+
2808+ if (! recordsPerCustomKey .containsKey (key )) {
2809+ recordsPerCustomKey .put (key , new List <SObject >());
2810+ }
2811+
2812+ recordsPerCustomKey .get (key ).add (record );
2813+ }
2814+
2815+ return recordsPerCustomKey ;
2816+ }
2817+
27292818 public Map <String , SObject > toMap (SObjectField keyField ) {
27302819 Map <String , SObject > recordPerCustomKey = (Map <String , SObject >) Type .forName (' Map<String, ' + this .ofObject + ' >' ).newInstance ();
27312820
0 commit comments