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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,338 views 14 20
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot