How to check if custom field is empty i.e. set to "none"? Edited

I am using this custom post script function to - add part of text between [... ]from custom field to summary field when creating sub-taks

So if in custom field is text [ACC]-accounting, only [ACC] part is added in field summary - at the begining before text that customer has written in summary field.

How can i check if custom field is empty, and set it so that nothing is added to field summary? 

This is code i am using

import com.atlassian.jira.component.ComponentAccessor;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11201")
def cFieldValue = issue.getCustomFieldValue(cField)
def a = cFieldValue.toString()
def x = a.indexOf('[')
def y = a.indexOf(']')
def newSummary = a[x+1..y-1] + " - " + issue.summary;
issue.summary = newSummary;

1 answer

0 vote

Use a simple if:

def cFieldValue = issue.getCustomFieldValue(cField)

if (cFieldValue) {
def a = cFieldValue.toString()
def x = a.indexOf('[')
def y = a.indexOf(']')
def newSummary = a[x+1..y-1] + " - " + issue.summary;
issue.summary = newSummary;

}

Ok--i used if in a similar way

import com.atlassian.jira.component.ComponentAccessor;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11201")
def cFieldValue = issue.getCustomFieldValue(cField)
def a = cFieldValue.toString()
def x = a.indexOf('[')
def y = a.indexOf(']')

if (cFieldValue !=null)

{def newSummary = a[x+1..y-1] + " - " + issue.summary;
issue.summary = newSummary;}

 

But what i don't understand in your code is:

- how do you check if cFieldValue is empty - there is no "!=null" part in your code ?

if (cFieldValue) {
def a = cFieldValue.toString()
def x = a.indexOf('[')
def y = a.indexOf(']')
def newSummary = a[x+1..y-1] + " - " + issue.summary;
issue.summary = newSummary;

}

And why can't i chechk if 'a' is empty? When i use if (a != null) then code isn't working?

 

Sorry but i am no programmer and i am struggling to program simple tasks in Jira

 

Ok, you're making an incorrect, but very human, assumption about logic here.

If I were to ask you a question with a "yes or no" answer (such as "are you left handed?"), how many possible answers are there?  Most people will look at the "yes or no" and tell you "two", but they're wrong.  There are three:  Yes, No, and "not an answer".

If your custom field is empty, or not valid for this issue, then issue.getCustomFieldValue(cField) is going to give you "Not an answer", because there's nothing there.

So, when that happens, your code for working out a, x and y cannot work, and a similar problem happens later in the process, you shouldn't be checking for nulls.

if (cFieldValue) is a simple way of saying "if there is anything in that variable", and you need to check that before trying to work out a, x and y.

ok thanks for info

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

650 views 6 12
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