[groovy] get id of created issue

SaidAslan August 1, 2016

Hi, 
On some transition I need to create a new issue and link it to current. 
issue is creating, but have faced with a little problem - I dont know how to take an issue key of this new created one. this code links current issue with itself, not a new one. 

could you help me please? 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
 
//to take current issue id
Issue issue = issue
def id=issue.getId()
 
//***rest code***
 
issueInputParameters.with {
projectId = projectManager.getProjectObjByKey("SD").id
summary = "test"
issueTypeId = issueType.getId()
description = "test"
 
Project project = projectManager.getProjectObj(projectId)
def projectlead=project.getLeadUserName()
reporterId =  projectlead
}

def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()
 
def issueResult = issueService.create(user, validationResult)
//to take created issue id
def newid=issue.getId()

def issueLinkManager = ComponentAccessor.getIssueLinkManager();
issueLinkManager.createIssueLink(newid, id, Long.parseLong("10003"),Long.valueOf(1), user);

1 answer

1 accepted

2 votes
Answer accepted
Thanos Batagiannis [Adaptavist]
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.
August 1, 2016

Hi Saida,

You can get the newly created issue via IssueValidationResult

def issueResult = issueService.create(user, validationResult)
def newIssue = issueResult.getIssue()
SaidAslan August 1, 2016

5 hours of searching and so simple answer!

thank you so much!

SaidAslan August 1, 2016

can you please explain me why it is returns 

ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SD-6574, actionId: 1031, file: <inline script>
groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script424
	at Script424.run(Script424.groovy:12)

when I change it like this? 

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
 
CustomField incAffect = customFieldManager.getCustomFieldObject(new Long(10703));
String curIncAffect = issue.getCustomFieldValue(incAffect);

if (issue.issueTypeObject.name == "Incident" &amp;&amp; (curIncAffect == "Group of branches" ||  curIncAffect == "Bank" || curIncAffect == "Branch"))
{
// code
}
incAffect is a single select list cf
SaidAslan August 1, 2016
or if
if (issue.issueTypeObject.name == "Incident" &amp;&amp; (cfValues['Incident Affected']?.getValue() == "Group of branches" ||  cfValues['Incident Affected']?.getValue() == "Bank" || cfValues['Incident Affected']?.getValue() == "Branch"))
{
// code
}

it returns :

2016-08-01 17:50:55,681 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SD-6574, actionId: 1031, file: <inline script>
groovy.lang.MissingPropertyException: No such property: cfValues for class: Script441
	at Script441.run(Script441.groovy:17)
Thanos Batagiannis [Adaptavist]
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.
August 1, 2016

You need to call the customFieldManager

import com.atlassian.jira.component.ComponentAccessor


def customFieldManager = ComponentAccessor.getCustomFieldManager()
// also you can get the custom filed by name instead of a long id
def incAffect = customFieldManager.getCustomFieldObjectByName("Inc Affect CustomField Name")

I think you asked another related AA question and the issue was that you were getting the customFieldManager but there was a typo...

regards

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events