Skip to main content
Skip table of contents

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 type attribute to define what kind of value is produced.

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 ("x" by default). The expression should resolve into a Number or a Currency value - any other type of value will generate an error. 

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 expr.

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):

  • currency - The resulting sum will be a Currency value.

  • number - The resulting sum will be a Number value.

  • autom - The resulting sum will be a Currency or a Number, matching the type of the items in the Collection.

 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.

CODE
<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.

CODE
<set var="myAmountSum" value="${0}"><forEach var="opp" value="${myOpportunities}"><set var="myAmountSum" value="${myAmountSum + opp.Amount}"> 
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.