0

I have an aggregate result to process OpportunityLineItems by custom field PackageId - using min, max, avg etc for most fields works fine.

However, for a text field I want to collect all the values into one string, probably csv, and then push into my output object.

Is this possible? thanks

I am trying to collect product_subtype__c from OpportunityLineItems that share a packageid__c field - my try to create a map:

Map<string,string> SubTypeMap = new map<string,string>();
List <opportunityLineItem> Records = [select PackageAdId__c, product_subtype__c
       from OpportunityLineItem
      where Opportunityid =: opp.id];

        for(opportunityLineItem record:records){
          SubtypeMap.put(record.PackageAdId__c,
          getValuesFromRecords(record.records, opportunityLineItem.Product_SubType__c));                      
                                        }

Produces this error:

Variable does not exist: records

2
  • Are you trying to do rollups? If you want to do string concatenation I would especially suggest DLRS. It has Concatenate and Concatenate Unique aggregate functions.
    – Adrian Larson
    Commented Feb 7, 2018 at 16:26
  • No, not a roll up - I generate a new custom object from the Product that will also be child to the Opportunity but isn't parent to the product.
    – jaw999
    Commented Feb 7, 2018 at 16:36

1 Answer 1

1

To get all of the values together by type, you will want to create a map. Here's how you might do that:

Map<String, Set<String>> subTypeValues = new Map<String, Set<String>>();
for(OpportunityLineItem record: records) {
  Set<String> subTypeSet = subTypeValues.get(record.PackageAdId__c);
  if(subTypeSet == null) {
    subTypeValues.put(record.PackageAdId__c, subTypeSet = new Set<String>());
  }
  subTypeSet.add(record.Product_SubType__c);
}

At this point, you've got a Set of values, so to convert them to a comma-separated list, you'd then go about converting them:

Map<String, String> subTypeStrings = new Map<String, String>();
for(String keyValue: subTypeValues.keySet()) {
  subTypeStrings.put(keyValue, String.join(new List<String>(subtypeValues.get(keyValue)),','));
}

After this step, your final map will contain values like:

'KeyA' => '001,002',
'KeyB' => '003',
'KeyC' => '002,004,005'
1
  • Excellent, thanks for working through! I see the map.
    – jaw999
    Commented Feb 7, 2018 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .