I have this postfunction script that creates a subtask and then assigns the subtask to different users depending on what is entered in a label custom field.
However it keeps failing, with the cfValue returning null.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.label.LabelManager
import org.apache.log4j.Category
def issueId = issue.getId()
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug: Assign sub-task"
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10284")
def cfValue = issue.getCustomFieldValue(customField) as String
if(("labelstring") in cfValue){
issue.setAssigneeId("person1")
log.info ("Custom field value: " + cfValue)
}
else
{ issue.setAssigneeId("person2")
log.info ("Custom field value: " + cfValue)
log.info ("Custom field: " + customField)}
(I'm leaving the logging in there).
I tried this way too:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.label.LabelManager
import org.apache.log4j.Category
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10284").getIdAsLong()
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
def labelMgr = ComponentAccessor.getComponent(LabelManager)
def labelValue = labelMgr.getLabels(issue, customField)
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug: Assign sub-task"
if(("somelabel") in labelValue){
issue.setAssigneeId("person1")
log.info ("Custom field value: " + labelValue)
}
else
{ issue.setAssigneeId("person2")
log.info ("Custom field value: " + labelValue)}
The custom field is returning, just not the value. Not sure if custom label fields require a different method?
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.user.ApplicationUser import com.atlassian.jira.issue.label.LabelManager Issue issue = issue def parentId = issue.getParentId() def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10284").getIdAsLong() def labelMgr = ComponentAccessor.getComponent(LabelManager) def labelValue = labelMgr.getLabels(parentId, customField) for (String label : labelValue) { if (!label.toLowerCase().contains("text")){ issue.setAssigneeId("person1")} else { issue.setAssigneeId("person2")} } if (issue.assigneeId == null) { issue.setAssigneeId("person2")}
Its a bit weird, but works. The issue was that I was pulling the label value from the subtask, not the parent issue.
The error is due to the fact that custom field of type Label returns a list instead of a string.
Please see here
From the comments in the above link
customer_list = (List<Option>) issue.getCustomFieldValue(custom_field);
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.
Thanks for the input @Tarun Sapra!
I've seen the second one, but it didn't work for me, see below. The first also through an error (List<Option>) could not resolve class "Option" haha. But now I know the label object value returns a list so thats something. Thanks !
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script351.groovy: 16: unexpected token: ? @ line 16, column 53. tCustomFieldValue(customField)?*.label ^
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.
Can you paste the exact code snippet which you are using right now.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure thing, thanks again for the help.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.issue.customfields.option.*
import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug: Assign sub-task"
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10284")
def cfValue = (List <Option>) issue.getCustomFieldValue(customField)
if(("string") in cfValue){
issue.setAssigneeId("person1")
log.info ("Custom field value: " + cfValue)
}
else
{issue.setAssigneeId("person2")
log.info ("Custom field value: " + cfValue)
log.info ("Custom field: " + customField)}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
this is wrong
if(("string") in cfValue){
issue.setAssigneeId("person1")
log.info ("Custom field value: " + cfValue)
}
You are trying to check a string in the list of options.
Can you please log/print cfValue right after the statement
def cfValue = (List <Option>) issue.getCustomFieldValue(customField)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah I know that condition is wrong, I haven't bothered updating it yet, since the custom field is still returning null.
Having the log.info before the condition still returns the same result... null.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So you mean to say that this statement is returning null?
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10284")
Can you print this and share the output
log.info issue.getCustomFieldValue(customField)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No. As I stated in the initial post, the custom field is returning properly, but the custom field value is not, and I've tried that way, and with the list <option> as described above and they both return null.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's why I suggested the following statement
log.info issue.getCustomFieldValue(customField)
So get an idea on the type of instance being returned by the above statement. So if you can share the output of the above statement.
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.