I have 2 string fields in database one is a name and the other is semicolon-separated values, I want to put the name and its summed up values in a map to use it later in the code.

I can split the sting field using .split(';') but I am not able to sum it up and then put it in the map key as a name field and aggregated sum value as the value in the map, please suggest

Thank you!

  • Why do you not want to use a loop to process a list?
    – David Reed
    Oct 9, 2019 at 17:07
  • Where are you getting the numbers that are in the list? If it's from a query on records, I'd suggest just using aggregate functions in SOQL. Oct 9, 2019 at 17:08
  • Your described functionality of "aggregate all the numbers" can mean a variety of things including sum, max, min, avg, etc. Please edit your post to be more specific.
    – Adrian Larson
    Oct 9, 2019 at 17:16
  • 1
    And David's initial question is still relevant. Why are you adamant that you want to avoid using a loop? Large amount of records, timeouts, etc? Oct 9, 2019 at 17:17

1 Answer 1


Given that you are storing a list of numbers in a delimited string field, "24; 35; 67", you have exactly two options. One is iteration (a loop); the other is recursion. You do not have the option of using aggregate queries or roll-up summary fields because you don't have actual number fields to look at.

Apex does not natively support a reduce() function, a list comprehension, or another such construct with which you may be familiar from other languages. Any such construct implemented in Apex will just be a facade over iteration or recursion.

Iteration is the natural approach here and should be used, barring changes to the data model. If you have broader needs that aren't articulated here, you may consider adding a child object to store an actual number field in a one-to-many relationship, which you can then address using SOQL aggregate queries and roll-up summary fields.

You must log in to answer this question.

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