sum
A logic command that provides an easy way to calculate the sum of values found within a Collection. An EL-expression is used to get a Number or a Currency value out of each item in the Collection, and the command produces a Number or Currency representing the sum of the found values.
Attributes
var | ||
---|---|---|
Required | Value type | EL-evaluated |
Yes | String | No |
Defines the name of the variable holding the sum, which is either a Number or a Currency value. Use the |
value | ||
---|---|---|
Required | Value type | EL-evaluated |
Yes | Collection | Yes |
Defines the Collection that contains values to be summed. The resolved value should therefore be a Collection whose items are either Numbers or Currency values, or values that can contain Numbers or Currency values, such as Data items or Maps. |
expr | ||
---|---|---|
Required | Value type | EL-evaluated |
Yes | Collection | No |
Defines the EL-expression that will be evaluated once for every item in the Collection. The Collection item can, and most likely should, appear in the expression with the variable name defined in |
itemName | ||
---|---|---|
Required | Value type | EL-evaluated |
No | String | No |
This attribute can be used to specify the name of the variable representing the Collection item in the EL-expression specified in If left undefined, the name of the item variable will be "x". |
type | ||
---|---|---|
Required | Value type | EL-evaluated |
No | String | Yes |
Defines the type of value this command produces. The resolved value is expected to be one of the following Strings (case insensitive):
If not defined, the value of "autom" is used. |
Examples
Common sources of Collections with items holding numerical values for sum
to handle are the commands retrieving data from Salesforce, query and relatedList. The following would produce a Currency value holding the sum of the Opportunities' Amount field values; with the type
attribute undefined and therefore using its default value "autom", the sum value is of the same type as the value representing Amount, which is Currency.
<query var="myOpportunities" select="SELECT Name, Amount FROM Opportunity WHERE OwnerId = '${UserInfo.userId}'"><sum var="myAmountSum" value="${myOpportunities}" expr="${opp.Amount}" itemName="opp">
The expr
attribute may seem a bit odd at first, but it should help to think of sum performing a loop like forEach and the expr
expression being used inside the loop. Here's a command structure mirroring the above sum
command, with the "opp.Amount" of expr
being used in the set within the forEach
loop.
<set var="myAmountSum" value="${0}"><forEach var="opp" value="${myOpportunities}"><set var="myAmountSum" value="${myAmountSum + opp.Amount}">