Forums

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

How to get a number value from a Scripted Field

Prasad A
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 25, 2023

Hi, 

I am new to ScriptRunner and Jira Software. Below is the details of the issue:

We have 2 scripted fields (ex: SF1 and SF2). The SF1 is a scripted number field whicg add's the number selected from 3 custom fields (CF1, CF2, CF3)

so SF1 = CF1+CF2+CF3. I am able to get the correct output for SF1.

The SF2 is also a Scripted number filed which should take the output from SF1 and divide with another Custom field (CF4) and show the output on the SF2

Ex: SF2 = SF1 + CF4 

The first part of the script (SF1 output) is working fine. But i am unable to find a method to pass the SF1 output as input for SF2.

Could anyone help me with any function on how to fetch the value from Scripted field and add to custom field?

Thanks in advance for help!

 

2 answers

0 votes
Radek Dostál
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 Leaders.
June 26, 2023

I'll nick Nic's reply from https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Possible-to-use-a-scriptrunner-script-field-value-in-another/qaq-p/681430

The easiest way to think of this is that "a scripted field value is calculated and stored during the indexing of the issue".  If you have more than one calculation happening during that indexing, then it's effectively random as to the order in which that happens.  Which means that sometimes, a calculation based on another scripted field might take the old value, or the new one, or, in some cases, even a random number!   The fields are recalculated even if their input data has not changed.

So, don't read scripted fields from the current issue in other scripted fields.  It won't work (reliably).  It's fine to read them from other issues, but never the current one.

 

It is far more reliable and easier to replicate your steps from SF1 inside SF2. The SF1's value will not be reliable "source". If SF1 changes, SF2 is likely to have incorrect value, and vice versa.

As a rule of a thumb, given how scripted fields work with indexes (and value "storage"), it's best to always avoid trying to read another scripted field's value.

0 votes
Evgenii
Community Champion
June 25, 2023

Hi, @Prasad A 

There is no secret oh hidden logic. Just take value from custom field SF1 and divide it with CF4.

There is one small, not obvious problem - values in issues appear only when they (issues) are opened by user, for example. If issue wasn't opened, values in customfields == null.

Of course, you can use workaround with sheduled reindexing of issues, it's not standart way to solve such problems, but it will help.

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 Leaders.
June 26, 2023

No, I'm afraid that is not a valid approach.  It is a mistake people inexperienced with Scriptrunner make a lot, but it's really not obvious, and whilst our docs mention it, most people won't try to to it, so we've not put it in a huge red box.

You should never read the value of a scripted field as an input to another.  Especially during the same update action.

Where the question is

  • SF1 = CF1+CF2+CF3
  • SF2 = SF1 + CF4 

the calculation for SF2 is likely to fail.  It needs to be rewritten as

  • SF2 = CF1+CF2+CF3 + CF4 

(Assuming Cf1-4 are all content-containing custom fields, not scripted fields)

Suggest an answer

Log in or Sign up to answer