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.

You can use only one property from a list in the Sum function. To combine multiple properties, use the Formula function.

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.

  1. 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.
    1. In K2 Designer, select the create SmartObject option.
    2. Specify Sales Order as the SmartObject Name.
    3. Specify a Description and select a Category.
    4. Leave the SmartObject Type as is then click Create.
    5. Add SmartObject properties as shown in the image below:
    6. Click Finish to complete and save your SmartObject.
  2. 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.
    1. On your Sales Order SmartObject page click Design a new View.
    2. Specify Sales List View as the Name, specify a Description, then select your Category.
    3. Select List View as the View Type.
    4. Click Create.
    5. Click Create Labels and Controls.
    6. Select all the SmartObject Field Names except for ID.
    7. Click Enable list editing and leave the default option of Edit all rows. This configures your view as an editable list view.
    8. Click OK.
    9. Click Finish to complete and save your view.
  3. Create a form to host your editable list view. If you are not familiar with creating forms, see How To: Create a Form.
    1. From your Sales List View page click Design a new Form.
    2. Specify Sales List Form as the Name, specify a Description, and select a Category.
    3. Click Create.
    4. Click (Enter View Title) and specify Sales as the title.
    5. Click Finish to complete and save your form.
  4. Create another view to add data to your Sales Orders SmartObject.
    1. From your Sales Order SmartObject page select Design a new View.
    2. Specify Sales - Add Invoices View as the Name. Specify a Description, and select a Category.
    3. Leave Item View as the View Type. Click Create.
    4. Include all the Field Names except for ID. Include the Create method button.
    5. Click OK.
    6. Click Finish to complete and save your view.
    7. Click Run to start the view.
    8. Add content to your SmartObject using the Sales - Add Invoices View, using the example values from the following image:

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.

  1. 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.
    1. On the Sales List Form page click Design a new Workflow.
    2. Specify Sales Total as the workflow name. Click Create.
    3. Expand the Start Events panel and click the Edit... option.
    4. Keep all the default options. Click Next, Next, Next, and Save and Finish.
    5. Click OK to complete the Start Event.
  2. Add a SmartObject reference step to the workflow.
    1. From the K2 Designer steps menu select SmartObject and navigate to your Sales Order SmartObject. In this scenario it is in Denallix.
    2. Drag and drop the Sales Order SmartObject to the workflow step.
    3. Expand the SmartObject configuration panel.
    4. Select Get List from the Method drop down, and ID as the Identifier.

  3. Add and configure an Email step with the Sum function in the email body.
    1. In K2 Designer, drag an Email step onto the design canvas and connect the workflow.
    2. Leave the email recipient as Originator. This sends the email to the person starting the workflow.
    3. Specify Sales Invoices Sub Total as the email Subject.
    4. 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.
    5. 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.)
    6. Close the SmartField Composer.
    7. Click File. Click Deploy. The workflow runs each time the view list form loads.
  4. Test your workflow by loading the Sales List Form.
    1. From the Sales List Form page click Run.
    2. The form loads the data present in the SmartObject then starts the workflow that sends the email.
    3. Check your outlook inbox. The email shows the sum of the Sub Total values.

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.

  1. 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.
    1. In K2 Designer, select your Sales Total workflow and click Edit.
    2. 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.
    3. Scroll down further in the Mathematics functions and drag and drop the Sum function to the Smartfield Composer Formula.
    4. 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.
    5. 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.
    6. 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.
    7. Close the Smartfield Composer and click File, then click Deploy. You can now test your modified workflow.
  2. Test your workflow by loading the Sales List Form.
    1. From the Sales List Form page click Run.
    2. The form loads the data present in the SmartObject then starts the workflow that sends the email.
    3. Check your outlook inbox. The email shows the total sum of the invoice Sub Total and Tax values.
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.