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

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

148 views 2 0
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