Forums

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

Scriptrunner - can't seem to use values of scripted fields in Groovy script

Peter Buyckx
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!
January 18, 2022

Hi all,

 

First of all, I'm not experienced nor skilled in coding in general, and certainly not in Groovy. I'm hoping to get some help in what I assume is a fairly simple script & question.

 

What I'm trying to do: create a scripted field that copies a value from another scripted field from a linked issue.

The script runs without any errors but always returns 0.0 as value. If I try copy the value of another (numeric) custom field from the linked issue, it works as expected.

 

Here's the code snippet (note: multiple linked issues may exist, so it starts with an iterator)

 

List<IssueLink> allInIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allInIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();

def linkedIssue = issueLink.getSourceObject()
String type = linkedIssue.getIssueType().getName();
if (type == "Delivery") {
CustomField fInvamt2 = customFieldManager.getCustomFieldObjectByName("Already Invoiced")
def iInvAmt = fInvamt2.getValue(linkedIssue) as Double

iAmount = iAmount + iInvAmt
}

return iAmount

 

kind regards & thanks for any help here!

Peter

 

 

1 answer

0 votes
PD Sheehan
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.
January 18, 2022

Is this your complete script?

I wonder if you have a scoping issue since you didn't declare iAmount.

But regardless ... you wrote Java for a tool that's designed to leverage groovy ... try this script instead:

import com.atlassian.jira.component.ComponentAccessor
def cfInvAmt = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Already Invoiced")[0]
def allInIssueLink = ComponentAccessor.issueLinkManager.getInwardLinks(issue.id);
allInIssueLink.sum{issueLink ->
def linkedIssue = issueLink.sourceObject
if(linkedIssue.issueType.name == 'Delivery'){
return linkedIssue.getCustomFieldValue(cfInvAmt) as Double
}
return 0
}

This uses more groovy approach:

  • don't use unnecessary getters
  • leverage groovy collection methods
  • don't force the data types unnecessarily

Also, I replaced the deprecated getCustomObjectByName with getCustomFieldObjectsByName

And finally adjusted the method to return the value of a custom field with the more standard approach for scriptrunner.

The final sum is what's returned implicitely. But if you want to log it or do other checks, put the sum into a variable, the explicitly return it.

Suggest an answer

Log in or Sign up to answer