Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner: Dependencies between calculated fields

mb
April 8, 2022

I want to calculate different fields by the selected values of an issue. But some of the calculated fields depend on other calculated fields, so I'm faced with a very complex set of calculations.

To stay with the core problem I will describe a simplified showcase:

  1. I have an issue with many input fields.
  2. I have a calculated field "score", that calculates a score from the input fields. So it depends only on the input fields.
  3. Now I want a calculated field "Key Figure A", that calculates a KPI from the score-field and some other input fields.
  4. Moreover I want some more calculated fields "Key Figure C...D" that calculate some more KPIs and ratings from the other key figures as well as other input fields.
  5. There are no circular dependencies between the figures and fields.

Because all calculations are very complex, I don't want to repeat the intermediate calculation steps in each of the descendant KPIs.

So my questions are:

  • Is it possible to access another calculated field in the code of a scripted field?
  • How does ScriptRunner behave, if I access another scripted field that depends on other scripted fields? Are there any problems to expect if one of the ancestors has to be calculated before the other fields?
  • Are there any performance or race conditions to expect?

I would appreciate your experiences, recommendation and alternative solution patterns.

Thank you very much.

1 answer

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
April 9, 2022

Whilst you technically can use a scripted field as one of the inputs to another scripted field, you'll find it does not work.

You can't guarantee what order scripted fields are going to be calculated in, so you could easily end up with the wrong answer in the dependent one.

Imagine you create fields:

Weekend:  a scripted field that looks at the due date and holds 0 for a weekday, 2 when it's a Saturday and 1 when it's Sunday.

Working due date: a scripted date field that is (due date + weekend day value)

Let's say someone puts in a due-date that lands on a Saturday.  Working due date should be Monday, but sometimes its going to show Saturday because the calculation of the weekend number happens after the working due date.  You can't control that.

You might be better off changing your first layer of scripted fields into custom fields, and populating them in a listener that picks up create and edit events (don't put them on the create or edit screens - that will stop people changing them).  Then your scripted fields that use their values as inputs will work fine, because custom fields are set before the scripted fields start to execute.

mb
April 11, 2022

Thank you for answer.

You recommend listeners and listening for update events. We already have some listeners for update events. To propagate all KPIs do you recommend a new listener for each KPI or do you recommend integrating all KPI calculations into an existing update listener?

My question is also about possible race conditions as well: I'm affraid that if different listeners populate various fields at the same time and dispatch the change event so I will get lost in a hell of events.

Suggest an answer

Log in or Sign up to answer