Forums

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

not able to assign issue through script runner

Pravin July 30, 2021

I am using below code to create the ticket in Jira .

 

but i am not able to pass user name to setAssigneeId(12345) in below code getting not such property exception

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager

// the project key under which the issue will get created
final projectKey = 'TEST'

// the issue type for the new issue
final issueTypeName = 'Bug'

// user with that user key will be the reporter of the issue
final reporterKey = 'auser'

// the summary of the new issue
final summary = 'Groovy Friday'

// the priority of the new issue
final priorityName = 'Major'

def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager)

def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
assert project : "Could not find project with key $projectKey"

def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
assert issueType : "Could not find issue type with name $issueTypeName"

// if we cannot find user with the specified key or this is null, then set as a reporter the logged in user
def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey) ?: loggedInUser

// if we cannot find the priority with the given name or if this is null, then set the default priority
def issueContext = new IssueContextImpl(project, issueType) as IssueContext
def priorityId = constantsManager.priorities.findByName(priorityName)?.id ?: prioritySchemeManager.getDefaultOption(issueContext)

def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(project.id)
setIssueTypeId(issueType.id)
setReporterId(reporter.name)
setSummary(summary)
setPriorityId(priorityId)
}

def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def result = issueService.create(loggedInUser, validationResult)
assert result.valid : result.errorCollection

 

 

 

1 answer

1 accepted

1 vote
Answer accepted
Vikrant Yadav
Community Champion
July 30, 2021

Hi @Pravin  Your Code is correct , add three lines to set Assignee :- 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager

// the project key under which the issue will get created
final projectKey = 'VY'

// the issue type for the new issue
final issueTypeName = 'Bug'

// user with that user key will be the reporter of the issue
final reporterKey = 'vikrant-yadav2'

// the summary of the new issue
final summary = 'Groovy Friday'

// the priority of the new issue
final priorityName = 'Major'

//assignee
final assigneekey = 'vikrant-yadav2'

def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager)

def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
assert project : "Could not find project with key $projectKey"

def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
assert issueType : "Could not find issue type with name $issueTypeName"

// if we cannot find user with the specified key or this is null, then set as a reporter the logged in user
def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey) ?: loggedInUser
def assignee = ComponentAccessor.userManager.getUserByKey(assigneekey) ?: loggedInUser

// if we cannot find the priority with the given name or if this is null, then set the default priority
def issueContext = new IssueContextImpl(project, issueType) as IssueContext
def priorityId = constantsManager.priorities.findByName(priorityName)?.id ?: prioritySchemeManager.getDefaultOption(issueContext)

def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(project.id)
setIssueTypeId(issueType.id)
setReporterId(reporter.name)
setSummary(summary)
setPriorityId(priorityId)
setAssigneeId(assignee.name)
}

def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def result = issueService.create(loggedInUser, validationResult)
assert result.valid : result.errorCollection

 Hope it works for you. 

 

Thanks

Pravin July 30, 2021

Hi @Vikrant Yadav 

 

Thank you for your reply .

but unfortunately getting below error .

 

groovy.lang.MissingPropertyException: No such property: userManager for class: Script323 at Script323.run(Script323.groovy:11)

 

Thanks ,

Pravin  

Vikrant Yadav
Community Champion
July 30, 2021

Add :-

 

import com.atlassian.jira.user.util.UserManager

thanks 

Pravin July 30, 2021

Hi @Vikrant Yadav ,

yes it is working now .
Thank you so much for your help .

Regards,

Pravin 

Like Vikrant Yadav likes this
Vikrant Yadav
Community Champion
July 30, 2021

😎 Glad to hear it works for you! 

Like Pravin likes this

Suggest an answer

Log in or Sign up to answer