Sorting on scripted custom field

Jeff Tillett
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2019

I have a custom scripted field to count the number of issue links and dump the value into a custom field for the purposes of reporting. The goal is to be able to sort issues descending by the amount of linked issues. 

This is my field code:

import com.atlassian.jira.component.ComponentAccessor


a = ComponentAccessor.getIssueLinkManager().getLinkCollection(issue, issue.reporter).getAllIssues().size()

a.toString()

Everything works as expected except when I sort. I have one issue with 34 links, and it comes WAY down in the list after issues that only have 1 or 2 links. 

What I can observe happening, is that Jira is sorting by the first digit only. For example, sorting by this field DESC yields the following pattern:

14

10

1

1

22

2

2

34

3

31

 

as an example. 

1 answer

1 accepted

3 votes
Answer accepted
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.
January 23, 2019

Your field output is not a number, it's a string.  To fix it:

  • Change the type of the field from "string" to "number".  This is in the scripted field settings under the script.
  • The searcher needs to be a number range (or number, although I've never really found a use for that).   That's in Custom fields -> your field -> Edit
  • Change the return statement in your script to return a number (might need to cast it to double or long, I can never remember)
Jeff Tillett
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 24, 2019

Thank you Nic! I had the first bullet done, but not the second. Once I did that and re-indexed, everything was perfect!

Vineela Durbha July 26, 2020

@Nic Brough -Adaptavist- 

Hi Nic,

I have configured my Scripted field as number ,but still it doesnt sort properly. What needs to be done now?

Thomas Litherland
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!
April 30, 2024

Commenting late for anyone who still faces this issue but potentially misses what Nic referred to relating to the Searcher type above.

  • Scriptrunner's Fields section/tab, when searching for your affected field, before you click on Edit, or any other option, you will see a Searcher type for the field. In some cases this will be be None or Free Text Searcher. This needs to be set to Number Searcher to ensure the JQL search will interpret the values loaded for this field can be sorted correctly. This was the final thing I had not corrected that once updated fixed my issue.

Suggest an answer

Log in or Sign up to answer