How To: Use the Sum function
You can use the Sum function in a workflow to get the total for a set of values. For example, you can reference the Get List method for a SmartObject to retrieve a list of records, and then use a property from the list to calculate the sum. In this example, we calculate the sum of Sub Totals for invoices to arrive at a total outstanding amount , and then insert this total value into an email as part of a workflow.
Using the Sum function to total the value of sales orders
Scenario
You have a K2 application that stores sale invoice information. Your SmartObject stores a subtotal value of each invoice. You need to know the total sub-totals of all invoices and insert this value into an email that is sent by a workflow. (We also describe how to use the Formula function to calculate the sum of sub-totals and tax amounts for all invoices.)
Steps
You create a K2 application for your sales invoices with a SmartObject, editable list view, and associated form. Then you create a workflow using the SmartObject's Get List method reference to sum the values of each Sub Total SmartObject property. You use that total in an email step.
You then modify the workflow email step to use a Formula function to calculate the total of the sub total and tax values summed together.
Setup Steps
The following steps set up the scenario from start to finish. If you want to skip the setup steps and view the topic step, skip to Create a workflow that uses the Sum function to calculate the sum for a property.
- Create a Sales Order SmartObject to store your sales invoice content.
If you are not familiar with creating a SmartBox SmartObject, see How To: Create A SmartBox SmartObject.
- In K2 Designer, select the create SmartObject option.
- Specify Sales Order as the SmartObject Name.
- Specify a Description and select a Category.
- Leave the SmartObject Type as is then click Create.
- Add SmartObject properties as shown in the image below:
- Click Finish to complete and save your SmartObject.
- In K2 Designer, select the create SmartObject option.
- Create an editable list view for your Sales Order SmartObject.
If you are not familiar with creating views, see How To: Create an Item View and a List View.
- On your Sales Order SmartObject page click Design a new View.
- Specify Sales List View as the Name, specify a Description, then select your Category.
- Select List View as the View Type.
- Click Create.
- Click Create Labels and Controls.
- Select all the SmartObject Field Names except for ID.
- Click Enable list editing and leave the default option of Edit all rows. This configures your view as an editable list view.
- Click OK.
- Click Finish to complete and save your view.
- On your Sales Order SmartObject page click Design a new View.
-
Create a form to host your editable list view.
If you are not familiar with creating forms, see How To: Create a Form.
- From your Sales List View page click Design a new Form.
- Specify Sales List Form as the Name, specify a Description, and select a Category.
- Click Create.
- Click (Enter View Title) and specify Sales as the title.
- Click Finish to complete and save your form.
- From your Sales List View page click Design a new Form.
- Create another view to add data to your Sales Orders SmartObject.
- From your Sales Order SmartObject page select Design a new View.
- Specify Sales - Add Invoices View as the Name. Specify a Description, and select a Category.
- Leave Item View as the View Type. Click Create.
- Include all the Field Names except for ID. Include the Create method button.
- Click OK.
- Click Finish to complete and save your view.
- Click Run to start the view.
- Add content to your SmartObject using the Sales - Add Invoices View, using the example values from the following image:
- From your Sales Order SmartObject page select Design a new View.
Use the Sum function in a workflow to calculate the sum for one property
The steps below explain how to configure the Sum function using a Get List SmartObject reference in a workflow.
-
Create a workflow that uses the Sum function to send an email with the summed total of the Sub Total values of the Sales Order SmartObject.
- On the Sales List Form page click Design a new Workflow.
- Specify Sales Total as the workflow name. Click Create.
- Expand the Start Events panel and click the Edit... option.
- Keep all the default options. Click Next, Next, Next, and Save and Finish.
- Click OK to complete the Start Event.
- On the Sales List Form page click Design a new Workflow.
- Add a SmartObject reference step to the workflow.
- From the K2 Designer steps menu select SmartObject and navigate to your Sales Order SmartObject. In this scenario it is in Denallix.
- Drag and drop the Sales Order SmartObject to the workflow step.
- Expand the SmartObject configuration panel.
- Select Get List from the Method drop down, and ID as the Identifier.
-
Add and configure an Email step with the Sum function in the email body.
- In K2 Designer, drag an Email step onto the design canvas and connect the workflow.
- Leave the email recipient as Originator. This sends the email to the person starting the workflow.
- Specify Sales Invoices Sub Total as the email Subject.
- Select the Functions tab in the Context Browser, expand the Mathematical functions and scroll down to Sum (Number). Drag the Sum function to the body of the email.
- Select the Fields tab in the Context Browser and then expand the Sales Order reference. Drag and drop the Sub Total field into the Values field in the Sum function. (The reference comes from the Get List SmartObject step, and will return the values of each invoice's Sub Total to the Sum function.)
- Close the SmartField Composer.
- Click File. Click Deploy. The workflow runs each time the view list form loads.
- In K2 Designer, drag an Email step onto the design canvas and connect the workflow.
-
Test your workflow by loading the Sales List Form.
- From the Sales List Form page click Run.
- The form loads the data present in the SmartObject then starts the workflow that sends the email.
- Check your outlook inbox. The email shows the sum of the Sub Total values.
- From the Sales List Form page click Run.
Use the Formula function in a workflow to calculate the sum of multiple properties
The sales order SmartObject also stores the amount of tax charged per invoice. Suppose that you need to know the total value of the sub total and tax amounts - in other words, you need to add the sums of two separate properties together. You can use the Formula function to do this.
- To configure multiple Sum values, like the sum of the invoice sub totals and the tax together, use the Formula function. Modify the Sales Total workflow email to use the Formula function with two Sum functions added together.
- In K2 Designer, select your Sales Total workflow and click Edit.
- Select the Email step and modify the Subject and Body as shown below. Expand the Context Browser, click Functions, expand the Mathematics functions and drag and drop a Formula function to the email body.
- Scroll down further in the Mathematics functions and drag and drop the Sum function to the Smartfield Composer Formula.
- In the Context Browser select the Fields tab, expand the Sales Order SmartObject reference and drag and drop the Sub Total property into the Values box. This sums the Sub Total values from the SmartObject list.
- In the Formula box create a sum (addition) formula by entering a sum (+) symbol. Then drag and drop a second Sum function into the Formula. This will add the two Sum functions together.
- In the Context Browser select the Fields tab. Expand the Sales Order SmartObject reference and drag and drop the Tax property to the Values box. This sums all the Tax values from the SmartObject list.
- Close the Smartfield Composer and click File, then click Deploy. You can now test your modified workflow.
- In K2 Designer, select your Sales Total workflow and click Edit.
- Test your workflow by loading the Sales List Form.
- From the Sales List Form page click Run.
- The form loads the data present in the SmartObject then starts the workflow that sends the email.
- Check your outlook inbox. The email shows the total sum of the invoice Sub Total and Tax values.
- From the Sales List Form page click Run.
Review
You created a K2 application for your sales invoices with a SmartObject, editable list view, and associated form. Then you created a workflow that used the SmartObject's Get List method reference in a Sum function to sum the values of the Sub Total SmartObject property. You use that total in an email step to send the total invoiced value to your email account. Finally you modified the workflow email step to use a Formula function to present the total of the sub total and tax values summed together.