Hi,
I am trying to create a new issue from an existing one through scriptrunner postfunction. Below is my code
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.ApplicationUser
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DK-3814")
def groupManager = ComponentAccessor.getGroupManager()
String groupName = "ng-ngage-jira-users"
//****Declaration for Required Fields for Creation of Release****//
def assignee= issue.getAssigneeId()
def productVersion = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Product Version")
def productVersionValue = (String) issue.getCustomFieldValue(productVersion)
def ProductName = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Product Name")
def ProductNameValue = (String) issue.getCustomFieldValue(sdlProductName)
//****End of Declaration for Required Fields****//
//****Declaration for Optional Fields (user related) for Creation of Release****//
def Manager = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(" Manager")
def ManagerValue = issue.getCustomFieldValue(Manager)
IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters
.setProjectId(issue.getProjectObject().getId())
.setSummary(issue.summary)
.setDescription(issue.description)
.setIssueTypeId(issue.issueTypeId)
.setPriorityId(issue.priorityId)
.setReporterId(issue.reporterId)
.setAssigneeId(assignee)
.addCustomFieldValue(productVersion.getId(),productVersionValue)
.addCustomFieldValue(ProductName.getId(),ProductNameValue)
.addCustomFieldValue(Manager.getId(),ManagerValue)
IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters)
log.error("issue input parameters: " + issueInputParameters.getActionParameters())
if (createValidationResult.isValid())
{
log.error("createValidationResult")
IssueService.IssueResult createResult = issueService.create(user, createValidationResult)
if (!createResult.isValid())
{
log.error("Error while creating the issue.")
}
}
When I try to execute it I get an error. I am unable to figure out what is the mistake here. Can someone please guide?
The Issue class does not have a getPriorityId() method, but rather it has a getPriority() method. See https://docs.atlassian.com/software/jira/docs/api/7.2.2/com/atlassian/jira/issue/Issue.html
So instead of issue.priorityId you can use issue.priority.id
Thanks Payne. It worked!
One more question - how can I get the newly created issue ID. I am unable to fetch that in my code.
Can you help?
Thanks,
Aditya
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great! Glad it worked.
You can retrieve the newly-created issue ID from your createResult variable:
createResult.issue.id will give you the actual record ID (e.g. 87001)
createResult.issue.key will give you the issue key (e.g. SD-1234)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I added createResult.issue.key but it isn't working. Is there something that I am missing
Another issue stated appearing when I added all my custom fields is below. Any pointers?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My first guess would be that you're not creating the new issue. I would put the log.error("newIssue"+createResulet.issue.key) line in an else block, i.e.
if (!createResult.isValid())
{
log.error("Error while creating the issue.")
}
else
{
log.error("newIssue"+createResulet.issue.key)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.