[Script Runner] Post function to link back to issue

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
Henning Tietgens Community Champion Aug 06, 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

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

Useful answer, thanks!

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 Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

81 views 0 5
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