createIssueLink do not work

Hi all

jira 7.8.0 and script runner 5.3.16

I just recently started learning java and groovy.

This code is kind of a mess, however he in it all works except createIssueLink in doAfterCreate closure.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager

// edit the JQL to return the issues you want to clone

def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueFactory = ComponentAccessor.getIssueFactory()

MutableIssue curIssue = issue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def tempCF = customFieldManager.getCustomFieldObjectByName("Target clone project")
def checkbox_field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Target clone project")
def List<Option> getOptions(Issue issue, CustomField customField, List<String> optionList) {
    def config = customField.getRelevantConfig(issue)
    def options = ComponentAccessor.getOptionsManager().getOptions(config)
    def optionsToSelect = options.findAll { it.value in optionList }
}
     def listOfValues = curIssue.getCustomFieldValue(tempCF)
    def tempVAL = curIssue.getCustomFieldValue(tempCF);

def myFieldList2 = [];
for (myFieldName2 in listOfValues) {
    String strMFN = myFieldName2
    myFieldList2.add(strMFN)
}

for (myFieldName in listOfValues )
{

// clone the current issue
def newIssue = issueFactory.cloneIssue(issue)
    
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
//def issueManager = ComponentAccessor.getIssueManager()

doAfterCreate = {
    def issueToLinkTo = issueManager.getIssueByCurrentKey(newIssue.getKey())
        issueLinkManager.createIssueLink(curIssue.getId(), newIssue.getId(), 10001, 1, user)
 
}
    

String result = myFieldName

// set the project and any field you want to have a different value
def projectTo = ComponentAccessor.getProjectManager().getProjectByCurrentKey(result)
newIssue.setProjectObject(projectTo)
newIssue.setCustomFieldValue(checkbox_field, getOptions(issue, checkbox_field, myFieldList2))
    

Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>
issueManager.createIssueObject(user, newIssueParams)
    
log.info "Issue ${newIssue?.key} cloned to project ${projectTo.key}"
}

in line

issueLinkManager.createIssueLink(curIssue.getId(), newIssue.getId(), 10001, 1, user)

Cannot find matching method
com.atlassian.jira.issue.link.IssueLinkManager#createIssueLink{java.lang.Long java.lang,Long, int, int, com.atlassian,jira.user,ApplicationUser),
Please check if the declared type is right and if the method exists.

in the documentation:

https://docs.atlassian.com/software/jira/docs/api/7.8.0/com/atlassian/jira/issue/link/IssueLinkManager.html

Exist class IssueLinkManager and method createIssueLink.

Prompt in what there can be a problem and how to correct?

2 answers

Hi,

Can you try modifiying the code like below and try?

issueLinkManager.createIssueLink(curIssue.getId() as Long, newIssue.getId() as Long, 10001 as Long, 1 as Long, user)

 -Praveen

Thank! In your answer there is progress for me - static type checking stopped finding errors in line issueLinkManager.createIssueLink...

But the creation of linkages between the issue still doesn't work.

Static type cheking still say "The variable [doAfterCreate] is undeclared" - as far as I understand this is not an trouble?

Post-function works without errors, but the link is not created

Hi, 

You have written doAfterCreate in the code but havent declared it anywhere. You have to define it so that this error goes off. Also do you really require that "doAfterCreate"?

def doAfterCreate

Also do you really require that "doAfterCreate"?

-Yes. 

I would like to explain the ultimate goal:
In a loop I clone issue(curIssue) and using "doAfterCreate" I expect curIssue to be linked with newIssue(cloned issue).

Hi, 

Then you can just add 'def' before it, so that it is defined.

- Praveen

I did this:

def doAfterCreate = {
issueLinkManager.createIssueLink(curIssue.getId() as Long, newIssue.getId() as Long, 10001 as Long, 1 as Long, user)

}

And the above code does not work.

I don't understand . This example: https://community.atlassian.com/t5/Jira-questions/scriptrunner-clone-and-link-additional-action-to-clone-all/qaq-p/110551

Support scriptrunner use doAfterCreate without declare.

It seemed to me that doAfterCreate is a system variable.

Yes, you are correct. It is a system variable and doesnt have to be declared (sorry for the confusion caused).

@Thanos Batagiannis [Adaptavists] Requesting your help here. 

It seems since version 5.3.0 there is a bug in the documentation:

doAfterCreate documentation for 5.2.2:

https://scriptrunner.adaptavist.com/5.2.2/jira/builtin-scripts.html#_after_create_actions

doAfterCreate documentation for 5.3.16:

https://scriptrunner.adaptavist.com/5.3.16/jira/builtin-scripts.html#_after_create_actions

@Praveen while waiting for a response from support for scriptrunner. Could you check the following statement?

The above script is placed in post-function which is bound to transition "create".
Do I understand correctly that at the moment of creation issue 

on transition "create" curIssue.getId() exist

however on transition "create" newIssue.getId() not exist

This is the right statement?

I will explain what my question is related to: 

I saw that on transition "create" curIssue.getId() NOT EXIST

image.png

curIssue.getId() should work because the current issue already exists.

-Praveen

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Apr 30, 2018 in Marketplace Apps

Why moving to Confluence Data Center is only a job half-done

Confluence Data Center is a deployment option that was designed for performance at scale. In other words, Confluence will handle increased traffic much reliably in a clustered environment and yo...

237 views 0 6
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