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

This widget could not be displayed.

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 Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

536 views 3 19
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