Groovy to create issues

luanlopes94 June 11, 2018

Hello everyone,

I am trying to create a new issue by groovy script in post function. I added all necessary fields but when I execute validateCreate function I received the error:

Error Messages: [Summary: You must specify a summary of the issue.

My code is:

IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

issueInputParameters
    .setProjectId( issue.getProjectObject().getId() )
    .setSummary( "[Rel Request]" )
    .setDescription(transitionComment)
    .setIssueTypeId(issueTypeID)
    .setPriorityId(parentPriority)
    .setReporterId(reporterID)
    .setAssigneeId(assigneeID)
    
log.error("Summary: " + issueInputParameters.getSummary())

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

CreateValidationResult createValidationResult =
        issueService.validateCreate(user, issueInputParameters)
        
if (createValidationResult.isValid())
{
    log.error("entrou no createValidationResult")
    IssueResult createResult = issueService.create(
            user, createValidationResult)
    if (!createResult.isValid())
    {
        log.error("Error while creating the issue.")
    }
}

What's the error? Anybody can help me?

Thank you guys.

2 answers

1 accepted

1 vote
Answer accepted
Mark Markov
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.
June 11, 2018

Hello @luanlopes94

I check your code  in script console and it work perfectly

Are you sure that other parameters is not nulls?

There example of code i check

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

IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DS-2")
issueInputParameters
.setProjectId( issue.getProjectObject().getId() )
.setSummary( "[Rel Request]" )
.setDescription("test desc")
.setIssueTypeId(issue.issueTypeId)
.setPriorityId(issue.priorityId)
.setReporterId(issue.reporterId)
.setAssigneeId(issue.assigneeId)

log.error("Summary: " + issueInputParameters.getSummary())

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

IssueService.CreateValidationResult createValidationResult =
issueService.validateCreate(user, issueInputParameters)

if (createValidationResult.isValid())
{
log.error("entrou no createValidationResult")
IssueService.IssueResult createResult = issueService.create(
user, createValidationResult)
if (!createResult.isValid())
{
log.error("Error while creating the issue.")
}
}
luanlopes94 June 11, 2018

Mark,

Thank you for the tip. I detected a problem with setIssueTypeId. I am trying to pass a custom issue type by parameter like this:

def issueTypeID = "10013"

But, not working. If a use issue.issueTypeId it's work correctly. Do you know the correct way to pass issue type information?

Thank you.

Mark Markov
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.
June 11, 2018

I check code with declaration like in your case and it work:

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

IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
def issueTypeID = "10103"
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DS-2")
issueInputParameters
.setProjectId( issue.getProjectObject().getId() )
.setSummary( "[Rel Request]" )
.setDescription("test desc")
.setIssueTypeId(issueTypeID)
.setPriorityId(issue.priorityId)
.setReporterId(issue.reporterId)
.setAssigneeId(issue.assigneeId)

log.error("Summary: " + issueInputParameters.getSummary())

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

IssueService.CreateValidationResult createValidationResult =
issueService.validateCreate(user, issueInputParameters)

if (createValidationResult.isValid())
{
log.error("entrou no createValidationResult")
IssueService.IssueResult createResult = issueService.create(
user, createValidationResult)
if (!createResult.isValid())
{
log.error("Error while creating the issue.")
}
}

Please check that issue type with given id exist, and .setIssueTypeId() need id as string object, may be there some problem with groovy cast dynamic types.

Try to declare it directly :

String issueTypeID = "10013"
luanlopes94 June 11, 2018

The error is the same with String object:

Summary: You must specify a summary of the issue.

Issue types exists and I am passing a String to setIssueTypeId().

I don't know :(

Mark Markov
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.
June 11, 2018

Try to log everything and see thats all parameters correct:

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
import org.apache.commons.lang3.StringUtils

IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
def issueTypeID = "10103"
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DS-2")
issueInputParameters
.setProjectId( issue.getProjectObject().getId() )
.setSummary( "[Rel Request]" )
.setDescription("test desc")
.setIssueTypeId(issueTypeID)
.setPriorityId(issue.priorityId)
.setReporterId(issue.reporterId)
.setAssigneeId(issue.assigneeId)

log.error("Summary: " + issueInputParameters.getSummary())

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

IssueService.CreateValidationResult createValidationResult =
issueService.validateCreate(user, issueInputParameters)
log.error("issue input parameters: " + issueInputParameters.getActionParameters())
if (createValidationResult.isValid())
{
log.error("entrou no createValidationResult")
IssueService.IssueResult createResult = issueService.create(
user, createValidationResult)
if (!createResult.isValid())
{
log.error("Error while creating the issue.")
}
}
else
{
String cause = StringUtils.join(createValidationResult.getErrorCollection().getErrorMessages(), "/")
log.error("cause :" + cause)
}
luanlopes94 June 11, 2018

Thanks Mark,

Now the error is more specific:

2018-06-11 13:45:56,004 ERROR [workflow.ScriptWorkflowFunction]: Errors: {issuetype=The issue type selected is invalid.}

But, I am still with no solution :(

Mark Markov
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.
June 11, 2018

Hmmm, may be this issue type is subtask type?:)

For subtasks use 

validateSubTaskCreate instead of validateCreate.

luanlopes94 June 11, 2018

This issue type is normal task, not subtask.

The problem only occur when a set other value in IssueTypeId.

Mark Markov
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.
June 11, 2018

I give up :)

There is obviously some problem with issue type.

Try to create issue with another issueTypeId. Need some research :)

luanlopes94 June 11, 2018

Yeah, this is an issue type problem.

Other issue type works correctly :)

Mark, thanks for your time. I will verify the issue type restrictions/validations.

Mark Markov
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.
June 11, 2018

Thats right!

Do you try to create that issue type manually? 

Anyways if you find out the problem, let me know :) Im curious.

luanlopes94 June 11, 2018

When I try to create manually this error occur:

THIS TASK (SUB-TASK) TYPE IS NOT ALLOWED!

try to create from parent workflow button
1 vote
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 11, 2018

Hello @luanlopes94

If you have script runner plugin installed, then instead of writing the groovy scirpt, you can use the built-in function "clone issue" to create an issue with customized data and fields.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 11, 2018
Like tapraj likes this
luanlopes94 June 11, 2018

Tarun,

I can't use scriptrunner because my script needs to consume a rest service provided by a custom plugin.

Thank you for the tip.

Suggest an answer

Log in or Sign up to answer