Testing value of a custom field in Script Runner

Roy Chapman May 9, 2017

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
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 9, 2017

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"

Roy Chapman May 10, 2017

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
TAGS
AUG Leaders

Atlassian Community Events