Forums

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

Automatically assign particular user as a reporter

siva
Contributor
June 21, 2021

On issue creation I want to automatically assign the reporter with a specific person (i.e. not the person creating the issue), i,m trying to use script runner post function in create transition of workflow. I had a script that add particular user as watcher can any one modified the script to particular user assign to reporter.

Here the script below

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


def userName = "user name"
def userManager = ComponentAccessor.getUserManager()
MutableIssue issue = issue as MutableIssue
def user = userManager.getUserByName(userName)

def watcherManager = ComponentAccessor.getWatcherManager()

watcherManager.startWatching(user, issue)

 

Thanks in advance

Siva

2 answers

1 accepted

0 votes
Answer accepted
Vikrant Yadav
Community Champion
June 21, 2021

Hi @siva For this simple task , scripted post function is not required. 
simply use post function update issue fields . Select Reporter >> Add username 

For script runner :-

 

issue.reporterId = "xyz"

 

Thanks

siva
Contributor
June 21, 2021

Hi @Vikrant Yadav thanks for the reply,

I tried update issue field in post function but it didn't see the reporter as an option in drop down.

Thanks

Vikrant Yadav
Community Champion
June 21, 2021

Hi @siva  Then simply use script post function add below script :- It should work.

You are right reporter is not avialable in Update issue field. 

 

issue.reporterId = "xyz"

 Scripted post function.PNG

siva
Contributor
June 21, 2021

Hi @Vikrant Yadav  i used above script in post function but no changes done in reporter

reporter script.png

Thanks

Vikrant Yadav
Community Champion
June 21, 2021

Hi @siva  I have tested the same on my side. It's working fine. Are you using it on Create Issue transiton ?

If yes, then move it below Create Issue event. 

Check project permissions as well

Thanks

siva
Contributor
June 21, 2021

Hi @Vikrant Yadav  Yes, it's using on create transition and i move it below create issue event still no response in reporter field. Please let know where i did wrong.

Thanks

Vikrant Yadav
Community Champion
June 21, 2021

Hi @siva  Above script works for other transitions. For Create Issue use below script :- 

 

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 user = ComponentAccessor.getUserManager().getUserByName("vikrant-yadav2")

issue.setReporter(user)

 Hope it works for you. 

Vikrant Yadav
Community Champion
June 21, 2021

Keep it above Create Issue. 

Vikrant Yadav
Community Champion
June 22, 2021

Hi @siva Is it works for you ? 

Let me know the result.

siva
Contributor
June 22, 2021

Hi @Vikrant Yadav ,

issue.reporterId = "xyz"

 The above script works for me and it used in script runner post function create sub task. I used that script in create sub task in script runner.

Thank you very much for your help.

Regards,

Siva

Vikrant Yadav
Community Champion
June 22, 2021

@siva Cool! Glad to hear it works for you.  :)

Kindly mark the solution as Accepted. It helps other users having same problem. 

Thanks

V.Y 

0 votes
Hyrum Steffensen {Appfire}
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.
June 21, 2021

Hello Siva,

What about something like:

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.util.UserManager

IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue issue = issueManager.getIssueObject("DEMO-1");
UserManager userManager = ComponentAccessor.getUserManager()
def issueReporter = userManager.getUserByName("charlie")
issue.setReporter(issueReporter)
issueManager.updateIssue(issueReporter, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Please let us know how it goes!

Regards,

Hyrum

siva
Contributor
June 21, 2021

Hi @Hyrum Steffensen {Appfire} thanks for the response,

I tried your script and it throws the following error log

 

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2021-06-21 23:21:46,764 ERROR [workflow.AbstractScriptWorkflowFunction]: *************************************************************************************
2021-06-21 23:21:46,764 ERROR [workflow.AbstractScriptWorkflowFunction]: Script function failed on issue: NMS-77233, actionId: 1, file: null
java.lang.NullPointerException: Cannot invoke method setReporter() on null object
 at Script495.run(Script495.groovy:11)

Please let me know where to change script 

Thanks

Hyrum Steffensen {Appfire}
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.
June 21, 2021

Hello Siva,

I believe the problem is this line:

MutableIssue issue = issueManager.getIssueObject("DEMO-1");

You must specify an issue key that exists on your system instead of "DEMO-1". This may also work:

MutableIssue issue = issue as MutableIssue

Please note that you must also change this line to accept a username of a user that exists on your system.

def issueReporter = userManager.getUserByName("charlie")

Regards,

Hyrum

Suggest an answer

Log in or Sign up to answer