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

Lotus Support June 21, 2017

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 votes
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.
June 21, 2017

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;

}

Lotus Support June 21, 2017

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

 

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.
June 21, 2017

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.

Like Melisa L Smith likes this
Lotus Support July 4, 2017

ok thanks for info

Suggest an answer

Log in or Sign up to answer