Testing value of a custom field in Script Runner Edited

A number of projects use the same set of workflows, and we plan to keep it that way to stop divergence.  One team has asked that the field Story Points is mandated in a transition for their project, so I have tried to add a validator, as below;

import com.atlassian.jira.issue.Issue

def sp = customFieldManager.getCustomFieldObjectByName("Story Points")
if ( issue.projectObject.key == 'MAP' || issue.projectObject.key == 'DIGTST' && issue.getCustomFieldValue(sp).toString() == null ) {
return false;
}

The problem is that regardless of the content of Story Points (empty, 0 or a value > 0) it always returns false.  I realise that this statement is totally incorrect (not least because Story Points should be an integer)

issue.getCustomFieldValue(sp).toString() == null

I have also tried this method

cfValues['Story Points'] == ''

But get the same results

Any thoughts?

 

1 answer

1 accepted

0 vote

Three thoughts:

Instead of using ==, try the java .equals('MAP') construct - strings compared with == aren't always the same, even when they have the same content!

cfValues['Story Points'] should return a number, if memory serves, so try swapping the == '' to > 0

The other thought is to re-check your "if" statement - it currently says "if project 1 or project 2 and value is" - I think you probably want "if (project 1 or project 2) and value is"

Thanks Nic for the direction.  I have finally found the solution from various sources, this is the completed code (which I am sure would shame me and Groovy coders are biting at the bit to improve :-))

if ( issue.projectObject.key == 'MAP' || issue.projectObject.key == 'DIGTST' ) {

    if ( ( ( cfValues['Story Points'] ?:0 ) as Integer ) <= 0 ) {

               return false;

        }

    }

return true;

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

119 views 0 4
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