How To: Use the Sum function
Use the Sum function to get the total of a set of values. You first need to create a reference to a SmartObject (selecting the Get List method) and then use this in the Sum function. Note that you can use only one value per Sum function. To combine values and create a more complex formula, use the Formula function. For more information on how to use the Formula function, see the How to configure multiple Sum values in a formula section below.
Example of configured Sum function
The example uses data from a SmartObject called Sales Orders. In the workflow scenario, you create a Get List method reference and sum the values of each Sub Total SmartObject property. Then you append the sum value to the workflow folio using the Set Folio step.
- Create a SmartObject as shown below. This Sales Order SmartObject stores sales order data.
Once you create the SmartObject, populate it with some data by generating an editable list view.
- Create a new workflow called Sales.
- From the Toolbox, click the Basic category, and then drag the SmartObject Method step onto the canvas.
- Browse to and select the Sales Order SmartObject. From the Method drop down menu, select the Get List method. Select ID from the Identifier drop down.
- In the Output Mappings section, click the Create a Reference option to create a reference to the Sales Order SmartObject, which you then use in the Sum function.
- From the Toolbox, click the Basic category, and then drag the Set Folio step onto the canvas. Connect the step to the SmartObject Method step.
- Customize the folio field by entering 'Total Sales ' and then dragging the Sum function (found in the Mathematical section) from the Context Browser into this field.
- The Smartfield Composer shows. From the Fields section, expand the Sales Order reference and drag the Sub Total property into the Values field. Change the name of the Sum function to Sales Sub Total. In this example, the Sum function totals all Sub Total values.
For more information on how to use the Smartfield Composer, see the Smartfield Composer topic.
- Click X to close the Smartfield Composer. The final configuration on the folio field is as follows:
- Deploy the workflow.
For more information on deploying the workflow, see the Deploy a Workflow topic.
- From K2 Management, manually start an instance of the workflow. At runtime, the workflow calls the SmartObject, collects and calculates data, and then sets the workflow folio field with the total value of the Sub Total values. In this example the final sum is 600.
- After the workflow completes, click the Reports tab (in K2 Management) to view the All Instances report. The folio's value is Total Sales 600.
Like the first example, this one also uses a SmartObject called Sales Orders. In the workflow scenario, you create a Get List method reference along with a Formula function to calculate the sum of the Sub Total and Tax values. Finally, you append the result of the formula to the workflow folio using the Set Folio step.
- Create a SmartObject as shown below. This Sales Order SmartObject stores sales order data.
- Create a new workflow called All Sales.
- From the Toolbox click the Basic category, then drag the SmartObject Method step onto the canvas.
Once you create the SmartObject, populate it with some data by generating an editable list view.
- Browse to and select the Sales Order SmartObject. From the Method drop down menu, select the Get List method. Select ID from the Identifier drop down.
- In the Output Mappings section, select the Create a Reference option to create a reference to the Sales Order SmartObject, which you then use in the formula.
- From the Toolbox, click the Basic category, and then drag the Set Folio step onto the canvas. Connect the step to the SmartObject Method step.
- Customize the folio field by entering 'Total Sales and Tax ' and dragging the Formula function (found in the Mathematical section) from the Context Browser into this field.
- The Smartfield Composer shows. Drag the Sum function from Mathematical section within the Context Browser into the Formula field. From the Fields section expand the Sales Order reference and drag the Sub Total property into the Values field. Change the Sum function name to Sales Sub Total. In this example, the Sum function totals all Sub Total values.
- In the Formula field type a + (plus). In this example, the formula totals the Sub Total and Tax values.
- Drag another Sum function from the Context Browser into the Formula field. From the Fields section expand the Sales Order reference and drag the Tax property into the Values field. Change the Sum function name to Tax Total. In this example, the Sum function totals all Tax values.
- Click X to close the Smartfield Composer. The final configuration of the folio field is as follows:
- Deploy the workflow.
For more information on deploying the workflow, see the Deploy a Workflow topic.
- From K2 Management, manually start an instance of the workflow. At runtime, the workflow calls the SmartObject, collects and calculates data, and the sets the workflow folio field with the total value of the Sub Total and Tax properties. In this example, the sum of the Sub Total property is 600 and the sum of the Tax property is 60.
- After the workflow completes, click the Reports tab (K2 Management) and view the All Instances report. The Folio is set to Total Sales and Tax 660.