Forums

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

setReporter() function never works

Edward Greathouse
Contributor
August 22, 2018

I have a very easy script implemented as a post-function upon issue creation.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Origin")
def cfValue = issue.getCustomFieldValue(cf)

if (cfValue == 'Support') {
issue.setReporter(sthomson)
}

 

I have also tried issue.setReporter('Steve Thomson'), but that did not work either.

In addition, I did have logging in place and confirmed that "cfValue == 'Support'. Here is the script with the logging:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Level
import org.apache.log4j.Logger

def myLog = Logger.getLogger("com.onresolve.jira.groovy")
myLog.setLevel(Level.DEBUG)

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Origin")
def cfValue = issue.getCustomFieldValue(cf)

myLog.debug("Origins value: " + cfValue)

if (cfValue == 'Support') {
issue.setReporter(sthomson)
}
else {
myLog.debug("Did not update the reporter because value of Origin field was not Support")
}

Please help.

 

2 answers

1 accepted

2 votes
Answer accepted
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 22, 2018

Hello @Edward Greathouse

The thing is that method setReporter accepts ApplicationUser object. You ll need to get it first. Like this

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Origin")
def user = ComponentAccessor.getUserManager().getUserByName("Steve Thomson")
def cfValue = issue.getCustomFieldValue(cf)

if (cfValue == 'Support') {
issue.setReporter(user)
}

 

and place your postfunction before change history postfunction. 

Edward Greathouse
Contributor
August 22, 2018

Thank you Mark, I thought it was something simple like that.

One more thing, it appears I'm not making inside the "if" statement at all. Here are the logged messages:

2018-08-22 06:56:11,513 DEBUG [jira.groovy]: Origins value: Support
2018-08-22 06:56:11,513 DEBUG [jira.groovy]: Did not update the reporter because value of Origin field was not Support
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 22, 2018

Try to cast value to string directly, like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Origin")
def user = ComponentAccessor.getUserManager().getUserByName("Steve Thomson")
def cfValue = issue.getCustomFieldValue(cf).toString()

if (cfValue == 'Support') {
issue.setReporter(user)
Edward Greathouse
Contributor
August 22, 2018

I was able to get past the string comparison by casting the custom field value to a string. However, It appears the "user" is returning null.

@Mark Markov, the user "Steve Thomson" is an active directory user who is using Jira with his active directory credentials. Is there a special way for retrieving his user account?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Level
import org.apache.log4j.Logger

def myLog = Logger.getLogger("com.onresolve.jira.groovy")
myLog.setLevel(Level.DEBUG)

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Origin")
def user = ComponentAccessor.getUserManager().getUserByName("Steve Thomson")
def cfValue = issue.getCustomFieldValue(cf).toString()

myLog.debug("Origins value: " + cfValue)

if (cfValue == "Support") {
issue.setReporter(user)
myLog.debug("Updated reporter to: " + user)
}
else {
myLog.debug("Did not update the reporter because value of Origin field was not Support")
}



Here is the log:

2018-08-22 07:07:46,964 DEBUG [jira.groovy]: Origins value: Support
2018-08-22 07:07:46,964 DEBUG [jira.groovy]: Updated reporter to: null

 

Edward Greathouse
Contributor
August 22, 2018

Update*

I changed "Steve Thomson" to "sthomson" and I'm now getting this in the log:

2018-08-22 07:13:13,009 DEBUG [jira.groovy]: Origins value: Support
2018-08-22 07:13:13,025 DEBUG [jira.groovy]: Updated reporter to: sthomson(steve.thomson)

 
However, the issue still has the reporter as me when I create it. We are getting close. I will post screenshots of where I have the post function and the issue I tested it with.

Edward Greathouse
Contributor
August 22, 2018

order_of_post_funcitons.PNGissue_with_me_as_reporter.PNG

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 22, 2018

Okay, lets try to store changes manually via IssueManager

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Level
import org.apache.log4j.Logger

def myLog = Logger.getLogger("com.onresolve.jira.groovy")
myLog.setLevel(Level.DEBUG)

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Origin")
def user = ComponentAccessor.getUserManager().getUserByName("sthomson")
def cfValue = issue.getCustomFieldValue(cf).toString()

myLog.debug("Origins value: " + cfValue)

if (cfValue == "Support") {
issue.setReporter(user)
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
myLog.debug("Updated reporter to: " + user)
}
else {
myLog.debug("Did not update the reporter because value of Origin field was not Support")
}
Edward Greathouse
Contributor
August 22, 2018

Hi @Mark Markov,

I fixed it by adding the post funciton to the top of the que. Thank you so much for all the help!

post_function_on_top.PNG

0 votes
Edward Greathouse
Contributor
August 22, 2018

Fixed!

Once I moved the post function to the top of the list, I now see "Steve Thomson" as the reporter.

Thank you!

Suggest an answer

Log in or Sign up to answer