createIssueLink do not work

Maksim Smetannikov
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.
May 12, 2018

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

0 votes
Praveen May 14, 2018

Hi, 

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

- Praveen

smetannikov May 15, 2018

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.

Praveen May 15, 2018

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. 

Praveen May 15, 2018

You can find the documentation here (under After create options),

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

smetannikov May 15, 2018

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

smetannikov May 15, 2018

@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?

Maksim Smetannikov
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.
May 15, 2018

I will explain what my question is related to: 

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

image.png

Praveen May 15, 2018

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

-Praveen

0 votes
Praveen May 13, 2018

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

smetannikov May 14, 2018

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

Praveen May 14, 2018

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
smetannikov May 14, 2018

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events