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 votes
Accepted answer

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
Community showcase
Published Nov 29, 2018 in Marketplace Apps

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,378 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