Is this groovy Syntax correct(ish) for returning custom value if not null


Is this correct syntax? I know I am missing some import stuff, but I am reading the groovy documentation and hoping this would be correct?

def this = getCustomFieldValue("myField") ?: 0
If(this==0){//do stuff
Else{//do other stuff
return this


1 answer

Referencing some of my own, hopefully this generic, completely made up example helps to compare.  I'm honestly not really sure about your ?: 0

final String myField = "Custom Field A";

Object fieldTest = getCustomFieldValue(myField);

Integer total = 0;

if (fieldTest != null) { 
	total = (Integer) fieldTest;
else { 
	total = -1;
return total;

Hi Thanks for replying.

Well my attempt in using the Elvis Operator was to simplify the checking for null values and return a default value if it were in fact null. That dreaded null pointer exception. The reason I was asking about the syntax is due to the fact I am somewhat new to groovy and I wanted to make sure I was doing it correctly. Basically trying to leverage the nuances of the language to my advantage while learning new stuff.  

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Fadoua M. Boualem
Published Monday in Trello

Using Trello to manage events

As a Jira power user, I was at first doubtful that Trello could benefit my workflow. Jira already uses boards (ones you can customize!), so why would I even need to use Trello?! In this post you will...

502 views 8 9
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