Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Create a new Issue with data from an existing one

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 Community Leader Jan 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

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 Community Leader Jan 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

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 Community Leader Jan 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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you