Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Postfunction condition based on label custom field returning a null value

Aileen May 30, 2018

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?

2 answers

1 accepted

0 votes
Answer accepted
Aileen June 11, 2018
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.

0 votes
Tarun Sapra
Community Champion
May 30, 2018

The error is due to the fact that custom field of type Label returns a list instead of a string.

Please see here 

https://community.atlassian.com/t5/Answers-Developer-Questions/Get-values-of-label-based-custom-field-as-list-Groovy/qaq-p/553105

From the comments in the above link

customer_list = (List<Option>) issue.getCustomFieldValue(custom_field);
Aileen May 30, 2018

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
                                 ^

 

Tarun Sapra
Community Champion
May 30, 2018

You have to import the Option class.

Aileen May 30, 2018

ah yeah, duh. Sorry long day.

Still returning null.

Tarun Sapra
Contributor
May 30, 2018

Can you paste the exact code snippet which you are using right now.

Aileen May 30, 2018

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)}
Tarun Sapra
Community Champion
May 30, 2018

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)
Aileen May 31, 2018

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.

Tarun Sapra
Community Champion
May 31, 2018

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)
Aileen May 31, 2018

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.

Tarun Sapra
Community Champion
May 31, 2018

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. 

Aileen June 11, 2018

As I said in the above comment, the customfield name returned, the customfield value was null.

Suggest an answer

Log in or Sign up to answer