Possible to use a scriptrunner script field value in another script field?

Daniel Mezynski
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!
November 30, 2017

Hello,

 

Right now I have a script field generating a numeric value based on severity and priority. This number is being used in some reporting outside of Jira and for force ranking.

I have another field that I would like to use the numeric script field value to calculate a due date. 

Is it possible to get the value from another script field? When I try the following the script dumps out silently.

 

This code trying to pull from the other script field:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)


//Get CIF Value
log.debug "Getting CIF";
def cCIF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12708");
def CIF = issue.getCustomFieldValue(cCIF) as Integer;
log.debug "CIF is $CIF";

 

Results in this:

2017-11-30 14:38:19,578 DEBUG [acme.CreateSubtask]: Getting CIF

 

At the moment I have it working by including the first script field's logic in the second script field but it will lead to the codebases being likely to wander.

Alternatively could I have the first script field also set a regular custom field as it runs? I did not have any luck with that.

Thank you.

1 answer

1 vote
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.
December 1, 2017

You can, but you have to be careful not to go into a loop.

You can only rely on the value of a scripted field in another script if the current process is not going to update the value. 

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.

Samuel
Contributor
October 28, 2019

@Nic Brough -Adaptavist-  is there a way to show the scripted value in different issue type?

I have scripted field to rollup subtasks hours to epic but I also want to show the same value in another issuetype. I don't want to create another customField and copy the scripted field value. Thnx

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.
October 29, 2019

I am not clear on what you mean here.

If you mean that you want to show the same field on a different issue type, then just make sure the scripted field is in the issue view and has the right context.  This means that the script will be run for the issue you are looking at.

If you mean that you are calculating the field in (for example) issue ABC-123 and you want the result to appear on issue DEF-456 as well, then no, you will need a new script for project DEF - one that understands you want to look at ABC-123 and do the calculations from there.

Tomáš Vrabec
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.
December 20, 2022

Hi @Nic Brough -Adaptavist- just run into this, when trying to invoke calculated scripted field, getting always 100.0 as a result.

Is this still an issue in current version of script runner? 

Using 

 log.fatal linkedIssues.last().destinationObject.getCustomFieldValue(customFieldObjectivePoints)

where customFieldObjectivePoints is calculated.

This returns me always 100.0 even issue shows 23. 

Any ideas?

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.
December 21, 2022

It's still what I (nicked from the SR developers and) said before - do not rely on scripted field results until after you can be sure that their code has been run.

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events