Groovy Script Runner syntax for "is Null"

I have a JIRA custom field that is populated based on the value of another custom field. If the first field is null, I want the field I am populating to also be null. What is the proper syntax for "is null" in the first line shown below? It works without that line but will default to "Low". I need it to default to null or actually do nothing.

Thank you,
Jeannette

if ( issue.getCustomFieldValue(customField_name) = "")

return null

else if ( issue.getCustomFieldValue(customField_name) >= 6)

return "High"

else if ( issue.getCustomFieldValue(customField_name) <= 2)

return "Low"

else

return "Med"

2 answers

1 accepted

1 vote

Hi jnet,

I'm assuming that your source custom field is a Numeric Custom field and that you're using a scripted field.

In this scenario the correct code should be :

import com.atlassian.jira.issue.Issue;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.CustomFieldManager;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

Issue issue  = issue;

CustomField sourceCF = customFieldManager.getCustomFieldObjectByName("Source Field");

Integer sourceValue = issue.getCustomFieldValue(sourceCF);

if(sourceValue!=null){

	if(sourceValue&gt;=6)

        	return "High";

        else if (sourceValue&lt;=2)

         	return "Low";

        else

        	return "Med";

}

return "null";

Please, keep in mind that if the source value is not populated this script will display "null". If you want to not show this field if null please, write return "" instead of return "null' at the last script row.

Moreover, you need to change "Source Field" with the correct name of your custom field.

I hope this helps.

Fabio

Thank you, this worked perfectly! I now have a follow up question regarding my numeric custom field. please see https://answers.atlassian.com/questions/278603/which-custom-field-type-can-be-used-as-an-input-for-a-numeric-calculation#

1 vote

I'm not exactly sure what you are trying to do but in the other answer, 'return "null"' is almost certainly not what you want. Instead just: return null

Regarding proper syntax for is null - generally you just want to use groovy truth, and not distinguish between nulls and empty strings.

So I would just use:

if (! sourceValue) {

...

}

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
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

182 views 1 17
Join discussion

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