[groovy] get id of created issue

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

This widget could not be displayed.

Hi Saida,

You can get the newly created issue via IssueValidationResult

def issueResult = issueService.create(user, validationResult)
def newIssue = issueResult.getIssue()

5 hours of searching and so simple answer!

thank you so much!

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

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
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

511 views 3 18
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you