Following the same logic from 'Create a Calculating Form' which explains how to calculate form number fields and make the sum of the values at the end, here I'll explain how to calculate fields with a predefined value and leave a total value. So this is an option of the workaround from the features below:
And is meant to help projects where it is necessary to calculate a sum of numbers that was linked to a product or object and report the total. In this documentation, I used Jira Forms with Automation For Jira(A4J) and Assets. Also, I used as an example a form related to an order purchase from a store in which the products have predefined prices. So it will calculate the number of items, multiplies the values according to the number of selected items, add them to the fees provided, and then informs the total of the purchase. This method can be used for other purposes (eg. general and administrative costs).
Important related documentation:
How AFJ works
Smart values
Set up the Assets objects custom field
Create object schemas for each item that will be used, as well as for each option that will be used on the cascading logic, for my example I used: Product and Prices.
Remember to mark the option "Allow others to select objects from this schema". For more details about object schemas, please refer to Create an object schema
Create a custom field (assets) for each option that will be used in the cascading values, I created the field "Apple Product" and "Apple Product Price".
object HAVING inboundReferences(Name = ${customfield_XXXX.label})
Remember to mark the option "Display a default object when this field appears in a customer portal" to the refered field objects. For more details about object schemas, please refer to What is the Assets objects field?
Create an Asset object field for each item that will be used in the calculation, as well as for each field that will hold the result of a calculation.
For more information, please refer to the documentation Link a form field to a Jira field.
Extra tip: You can use conditional logic to better organize your purchase order. In this example, I created a field to select how many products the order will have, and depending on the answer, it will open the necessary lines to fill in the number of products. For more information, please refer to the documentation Add a conditional field to your project using Forms.
If your form will include nested calculations (for instance if you want to calculate a subtotal that is then used in a calculation of the grand total), then you will need a status change to trigger each calculation. A looping transition allows you to trigger the form to recalculate.
Note: This will create a "Calculate button":
For more details about looping transitions, please refer to How to create global looping transitions in a Jira workflow
Use the Edit issue fields action to perform the calculation. You will need to reference the custom field ID with the Assets object custom field in the rule, in this example I used the multiplication of the items and then their sum to calculate the subtotal:
{{#=}}{{issue.customfield_10104|0}}*{{issue.customfield_10169.Name|0}}+{{issue.customfield_10105|0}}*{{issue.customfield_10175.Name|0}}+{{issue.customfield_10106|0}}*{{issue.customfield_10176.Name|0}}+{{issue.customfield_10106|0}}*{{issue.customfield_10177.Name|0}}+{{issue.customfield_10107|0}}*{{issue.customfield_10178.Name|0}}+{{issue.customfield_10205|0}}*{{issue.customfield_10179.Name|0}}{{/}}
So the icing on the cake for calculating the total is to put the Subtotal on the end of the smart value order (This is because there is a timing issue on the automation rule, as the Subtotal is still being calculated and does not have the value to be added in the Edit action of the Total):
{{#=}}{{issue.customfield_10115|0}}+{{issue.customfield_10116|0}}+{{issue.customfield_10114|0}}{{/}}
The customfield_10114 is the Subtotal field. To find the custom field ID please take a look at this documentation.
Also, the "|0" smart value is necessary to put a "callback" value if the field is blank. So it will return the value 0 and it will not give you an error in your automation rule.
This workaround will work as shown in the screen recording below (note that it will always be necessary to refresh the page to give A4J enough time to execute the automation rule and the attached form must be open for the calculation to happen):
Bruno Altenhofen
Atlassian Engineer - The guy from Proforma(Jira Forms)
e-Core
Krakow - Poland
1 accepted answer
3 comments