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;
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.