Forums

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

Create a new Issue with data from an existing one

Aditya Sastry
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.
January 10, 2022

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?

error.png

1 answer

1 accepted

2 votes
Answer accepted
Payne
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.
January 10, 2022

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

Aditya Sastry
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.
January 10, 2022

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

Payne
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.
January 10, 2022

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)

Like Aditya Sastry likes this
Aditya Sastry
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.
January 10, 2022

I added createResult.issue.key but it isn't working. Is there something that I am missing

Untitled2.png

Another issue stated appearing when I added all my custom fields is below. Any pointers?

Untitled.png

Payne
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.
January 11, 2022

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)
}

Suggest an answer

Log in or Sign up to answer