[Script Runner] Post function to link back to issue

Błażej O_
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 6, 2013

Hello.

I'm trying to adapt a script from the following website:

http://www.denizoguz.com/2013/06/29/jiraautomatically-linking-issues-with-a-script-during-create-transition/

Even though I don't know groovy, nor java, I somehow (thanks to google and atlassian developer site) made it to the point where script enters succesfully the last "if". Now I'm experiencing the problem than I can't find solution to. One of the last statements of the code throws following error:

2013-08-07 11:16:37,977 Thread-997 DEBUG test_user 676x14687x1 fm4ix2 ***.***.***.*** /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.PostFunction] Script executed this code
2013-08-07 11:16:37,977 Thread-997 ERROR test_user 676x14687x1 fm4ix2 ***.***.***.*** /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: IssueLinkTypeManager for class: Script13
2013-08-07 11:16:37,977 Thread-997 ERROR test_user 676x14687x1 fm4ix2 ***.***.***.*** /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: IssueLinkTypeManager for class: Script13

I'm afraid I need some help. Can anybody tell me what I'm doing wrong? I also don't understand this statement:

def issueLinkTypeManager = componentManager.getComponentInstanceOfType(IssueLinkTypeManager.class);

Am I wrong or is it trying to get some parameter of variable during definition of the very same variable? Or is IssueLinkTypeManager (with capital "i") some api interface, and issueLinkTypeManager just a variable?

What should I do to make the code work properly? Here is the whole script:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
import org.apache.log4j.Category;
import com.atlassian.jira.issue.IssueManager;
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");
log.setLevel(org.apache.log4j.Level.DEBUG);

def ComponentManager componentManager = ComponentManager.getInstance();
def customFieldManager = componentManager.getCustomFieldManager();
def parentTaskIDCF = customFieldManager.getCustomFieldObject('customfield_12101');//write your own custom field id here
def parentTaskID = issue.getCustomFieldValue(parentTaskIDCF);
if (parentTaskID == null || parentTaskID == '') {
    return true;
} else {
    def issueManager = ComponentAccessor.getIssueManager();
    def parentIssue = issueManager.getIssueObject(parentTaskID);
    def isParentIssueValid = (parentIssue != null);
    if (isParentIssueValid) {
		log.debug "Script executed this code";
        def issueLinkManager = ComponentAccessor.getIssueLinkManager();
        def issueLinkTypeManager = componentManager.
                getComponentInstanceOfType(IssueLinkTypeManager.class);
        def issueLinkType = issueLinkTypeManager.
                getIssueLinkTypesByName('Relates').get(0);
        def authenticationContext = componentManager.
                getComponentInstanceOfType(JiraAuthenticationContext.class);
        issueLinkManager.createIssueLink(
                issue.id, parentIssue.id, issueLinkType.id, 1L, authenticationContext.user);
    }
}

Thanks in advance!

1 answer

1 accepted

3 votes
Answer accepted
Henning Tietgens
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 6, 2013

Try this:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import org.apache.log4j.Category

MutableIssue issue = issue
Category log = log

log.setLevel(org.apache.log4j.Level.DEBUG);

def ComponentManager componentManager = ComponentManager.getInstance();
def customFieldManager = componentManager.getCustomFieldManager();
def parentTaskIDCF = customFieldManager.getCustomFieldObject('customfield_12101');//write your own custom field id here
def parentTaskID = issue.getCustomFieldValue(parentTaskIDCF);
if (parentTaskID == null || parentTaskID == '') {
    return true;
} else {
    def issueManager = ComponentAccessor.getIssueManager();
    def parentIssue = issueManager.getIssueObject(parentTaskID);
    def isParentIssueValid = (parentIssue != null);
    if (isParentIssueValid) {
        log.debug "Script executed this code";
        def issueLinkManager = ComponentAccessor.getIssueLinkManager();
        def issueLinkTypeManager = componentManager.getComponentInstanceOfType(IssueLinkTypeManager.class);
        def issueLinkType = issueLinkTypeManager.getIssueLinkTypesByName('Relates').asList()[0];
        def authenticationContext = componentManager.jiraAuthenticationContext;
        issueLinkManager.createIssueLink(issue.id, parentIssue.id, issueLinkType.id, 1L, authenticationContext.getLoggedInUser());
    }
}

There was an import missing. I also made some small other adjustments. I assume that the parentTaskIDCF contains valid issue id, correct?
If this doesn't work it's always a good idea to throw some debug messages in to test the script.

log.debug "Parent ID: ${parentTaskID}"

Henning

Błażej O_
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 6, 2013

It works like a charm! Thank you very much!

Yes, issue id is always valid. I use database child custom field to retrieve it and make it read only. Also validating it against beeing "null".

christakacs
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 29, 2016

Useful answer, thanks!

Suggest an answer

Log in or Sign up to answer