How to set assignee and reporter on an issue created via ScriptRunner Programatically

Abdul May 6, 2021

Hi 

I am using following code to set the reporter of the issue that is being created

def issueInputParameters = issueService.newIssueInputParameters().with {
setIssueTypeId(currentIssueType.id)
setReporterId((ComponentAccessor.userManager.getUserByName("ReporterName") ).key)
setSummary(summary)
setPriorityId((constantsManager.priorities.findByName(priorityName) ?: constantsManager.defaultPriority).id)
}

 

Issue gets created correctly, however I have noticed that the "Reporter" and "Assignee" both are set to admin which is the user that executes the automation rule.

Question: How do I get the "ReporterName" assigned to the Reporter jira attribute.

Also, how do the set the assignee as well in the IssueInputParameters?

 

Thanks in advance.

 

1 answer

1 accepted

1 vote
Answer accepted
Mohamed Benziane
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 7, 2021

Hi,

I use this to set the reporter

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
log.warn(loggedInUser.name)

issueInputParameters        
.setProjectId(project.id)        
.setIssueTypeId("10001")        
.setReporterId(loggedInUser.name)

 I think is the same thing for the assignee. Make sure you do not have a automation rule that overwrite assignee and reporter field

Abdul May 9, 2021

Hi Mohamed,

 

Thank you for your response.

I tried your suggestion but still it is setting the admin user.

My code is as follows

===================================================

def validationResult;
ComponentAccessor.jiraAuthenticationContext.setLoggedInUser(loggedInUser) ;

def project = ComponentAccessor.projectManager.getProjectObjByKey(projectName);
def projectComponentManager = ComponentAccessor.getProjectComponentManager();
def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager

def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId((taskType == "Primary") ? project.id : parentIssue.projectObject.id)
setIssueTypeId(currentIssueType.id)
setReporterId(loggedInUser.name)
setSummary(summary)
setPriorityId((constantsManager.priorities.findByName(priorityName) ?: constantsManager.defaultPriority).id)
}

validationResult = (taskType == "Primary") ? issueService.validateCreate(loggedInUser, issueInputParameters) :
issueService.validateSubTaskCreate(loggedInUser, parentIssue.id, issueInputParameters);

def issueResult = issueService.create(loggedInUser, validationResult);

===================================================

Admin user is the one that is executing the automation rule and hence it is setting the same user as Reporter and Assignee.

LoggedInUser is not the admin user.  

In fact, I am creating primary and sub task from a CSV file which contains the reporter and assignee value.

What I want to achieve is that if the Reporter does not exists then set to "Anonymous"

and similary Assignee to "Unassigned"

John Chin
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.
May 9, 2021

Hi @Abdul ,

Based on the script, it seems there are more script above that set the loggedInUser

Alternatively, you can set and get the desired reporter or assignee manually. For example, declare another line to get the user information of the desired reporter or assignee.

import com.atlassian.jira.user.ApplicationUser

//get the desired reporter and assignee user information
ApplicationUser reporter = ComponentAccessor.getUserManager().getUserByName("kim") //eg: username is kim

ApplicationUser assignee = ComponentAccessor.getUserManager().getUserByName("loong") //eg: username is loong

...

//set the reporter and assignee

setReporterId(reporter.id)

setAssigneeId(assignee.id)

If the reporter value is returning a null basically setting Anonymous to the issue reporter. You may also verify the project is allowing Anonymous issue creation, see JIRAKB - How to allow users to create issues anonymously.

I hope this helps.

Suggest an answer

Log in or Sign up to answer