Cannot accurately sort by scripted field

Alex Christensen Community Champion Sep 17, 2015

I've created a scripted field called Work Effort Progress, but I'm not able to sort (ASC or DESC) on this field in the Issue Navigator unless I include "Work Effort Progress" is not EMPTY in the JQL. I don't think that I should need to do this because none of the fields have an empty value (i.e. "Work Effort Progress" is EMPTY doesn't return any issues at all). I've re-indexed multiple times and the issue is still occurring.

Here is the script I'm using.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category
import com.atlassian.jira.config.SubTaskManager
 
SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();
Collection subTasks = issue.getSubTaskObjects()
Double timeSpentTotal = 0
Double remainingEstimate = 0
Double progress = 0

if (subTaskManager.subTasksEnabled && !subTasks.empty) {
    subTasks.each {
        if (it.getTimeSpent() != null) {
            timeSpentTotal += it.getTimeSpent()
            }
        if (it.getEstimate() != null) {
            remainingEstimate += it.getEstimate()
            }
        }
    }
else if (!subTaskManager.subTasksEnabled || subTasks.empty) {
    if (issue.getTimeSpent() != null) {
        timeSpentTotal = issue.getTimeSpent()
        }
    if (issue.getEstimate() != null) {
        remainingEstimate = issue.getEstimate()
        }
    }
if (timeSpentTotal > 0) {
	progress = ( timeSpentTotal / ( timeSpentTotal + remainingEstimate ) ) * 100
}
return progress.round(2)

And here is my custom template (I added a pseudo progress bar far a visual representation of progress):

#if($value > 0.01 && $value <= 10)
    <div><strong>[<span style="color:green;font-weight:bolder">></span></strong>>>>>>>>>><strong>]</strong> $value%</div>
#elseif($value > 10 && $value <= 20)
    <div><strong>[<span style="color:green;font-weight:bolder">>></span></strong>>>>>>>>><strong>]</strong> $value%</div>
#elseif($value > 20 && $value <= 30)
    <div><strong>[<span style="color:green;font-weight:bolder">>>></span></strong>>>>>>>><strong>]</strong> $value%</div>
#elseif($value > 30 && $value <= 40)
    <div><strong>[<span style="color:green;font-weight:bolder">>>>></span></strong>>>>>>><strong>]</strong> $value%</div>
#elseif($value > 40 && $value <= 50)
    <div><strong>[<span style="color:green;font-weight:bolder">>>>>></span></strong>>>>>><strong>]</strong> $value%</div>
#elseif($value > 50 && $value <= 60)
    <div><strong>[<span style="color:green;font-weight:bolder">>>>>></span></strong>>>>>><strong>]</strong> $value%</div>
#elseif($value > 60 && $value <= 70)
    <div><strong>[<span style="color:green;font-weight:bolder">>>>>>></span></strong>>>>><strong>]</strong> $value%</div>
#elseif($value > 70 && $value <= 80)
    <div><strong>[<span style="color:green;font-weight:bolder">>>>>>>></span></strong>>>><strong>]</strong> $value%</div>
#elseif($value > 80 && $value <= 90)
    <div><strong>[<span style="color:green;font-weight:bolder">>>>>>>>></span></strong>>><strong>]</strong> $value%</div>
#elseif($value > 90 && $value < 100)
    <div><strong>[<span style="color:green;font-weight:bolder">>>>>>>>>></span></strong>><strong>]</strong> $value%</div>
#elseif($value == 100)
    <div><strong>[<span style="color:green;font-weight:bolder">>>>>>>>>>></span></strong><strong>]</strong> $value%</div>
#else
    <div><strong>[</strong>>>>>>>>>>><strong>]</strong> 0.0%</div>
#end

I can search on this field just fine and everything else about this field works wonderfully except for the sorting capabilities. When I try to sort, I get this error message:

"Error occurred communicating with the server. Please reload the page and try again."

If I add in the JQL mentioned above, I can sort just fine. Any ideas on what might be causing the error or where I can look to try to diagnose the issue? Thanks for your help!

3 answers

Which indexer/searcher is the custom field configured with?

0 votes
Alex Christensen Community Champion Sep 18, 2015

Hi, Jamie - this scripted field is using a Number range searcher.

I used the following field code:

def modulus = issue.id.mod(4)
modulus == 0 ? null : modulus as Double

and the sorting worked ok, except it sorted them in the order null, 3, 2, 1. Which is not intuitive to me.

If you're getting an error there should be a stack trace... although it might be truncated. Can you find it and post it?

Alex Christensen Community Champion Sep 21, 2015

Hi, Jamie - without making any changes to my field or anything today, I see that my field is sorting correctly. Not sure why it's working now and wasn't before. After creating the field, I re-indexed and checked, but it didn't work then. It is now! Appreciate your input and quick responses - thanks!

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Jira

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,095 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you