I'm wanting to build a for loop that can dynamically handle lists passed into a function for it.
Something along the lines of the following:
public static List<AggregateResult> aggregatePrep(List<sObject> records, Schema.DescribeFieldResult fieldToAggregate)
{
List<AggregateResult> returnList = new List<AggregateResult>();
Schema.SObjectType sot = records.getSObjectType();
SObject sObj = sot.newSObject() ;
SObjectField sf = fieldToAggregate.getSobjectField();
for(Schema.SObjectType.getSObjectType(records) so : records)
{
returnList.add(so);
//alternative to above is add(so.sf);
}
return returnList;
}
The issues I've run into are as follows:
1) using the for(Schema.SObjectType.getSObjectType(records) so : records)
I return the error expecting an equals sign, found '('
, so I suspect what I want may not be possible at all.
2) I try referencing sot directly as for(sot s : records)
and it states invalid type: sot
.
3) The alternative add of (so.sf); returns Field expression not allowed for generic SObject
, which is why I would like to find a way to dynamically iterate through a list of matching type for records
.
Is there any way to accomplish what I am trying to do? And if not, how should I handle this kind of situation, where I am trying to iterate through a list for items and add it to a different type of list?
Thanks!