Creating issue links using Clone issue and links post function, in creation transition (JIRA 6.1.2) Edited

Hello,

Here is my scenario:

When creating an issue A, I use the "Clone an issue and links" post function on the creation transition.
This creates a clone issue, issue B, on which I make some operations through "Additional issue actions":
- Update issue B summary, issue B fixverions
- Set issue B specific custom field to a specific value
- Create links between issue B and other issues

With the script below, all the actions related to fields are working, but not the links creation.
Important thing is that the same code works in other context (basic transition for example), and that I get the summary of my issue updated with the relevant Ids of the issues I want to link issue B to:

import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()

issue.summary = '[DASHBOARD] ' + sourceIssue.key + ' - ' + issue.summary
issue.fixVersions = new HashSet<String>()

def cfm = ComponentAccessor.getCustomFieldManager()
// customfield_27012 = LPT - Niveau de test
def cf_LPT_NiveauTest = cfm.getCustomFieldObject("customfield_27012")
def om = ComponentAccessor.getOptionsManager()
def cfConfig = cf_LPT_NiveauTest.getRelevantConfig(issue)
def option_DashBoardEvol = om.getOptions(cfConfig).getOptionForValue("DashBoard - Evol", null)
issue.setCustomFieldValue(cf_LPT_NiveauTest, option_DashBoardEvol ) 

//used for debug
issue.summary = "idLPTTdb=" + sourceIssue.getKey() + " " + issue.summary

issueLinkManager.getInwardLinks(sourceIssue.getId()).each {
  lpttdblink ->
	if (lpttdblink.getIssueLinkType().getName().contains('Traitement BSL')) {
		def idBSL = lpttdblink.getSourceObject()
		
		//used for debug
		issue.summary = "idBSL=" + idBSL.getKey() + " " + issue.summary
		
		issueLinkManager.createIssueLink(issue.getId(), idBSL.getId(), new Long(12000), null, authenticationContext.getLoggedInUser())
						
		idBSL.getSubTaskObjects().each {
			if ((it.getIssueTypeObject().getName().contains("[BSL] Evolution")) && (it.getSummary().contains("[LPI]"))) {
			
				//used for debug		
				issue.summary = "stBSL=" + it.getKey() + " " + issue.summary
				
				issueLinkManager.createIssueLink(issue.getId(), it.getId(), new Long(12000), null, authenticationContext.getLoggedInUser())
			}	
		}
	}
}

Can anyone help me on this?
Many thanks,

Best regards.

1 answer

So, issue linking operations need to be put in the doAfterCreate closure, as described in the docs. So

doAfterCreate = {
    issueLinkManager.getInwardLinks(sourceIssue.getId()).each {
        lpttdblink ->
            if (lpttdblink.getIssueLinkType().getName().contains('Traitement BSL')) {
                def idBSL = lpttdblink.getSourceObject()

                //used for debug
                issue.summary = "idBSL=" + idBSL.getKey() + " " + issue.summary

                issueLinkManager.createIssueLink(issue.getId(), idBSL.getId(), new Long(12000), null, authenticationContext.getLoggedInUser())

                idBSL.getSubTaskObjects().each {
                    if ((it.getIssueTypeObject().getName().contains("[BSL] Evolution")) && (it.getSummary().contains("[LPI]"))) {

                        //used for debug  
                        issue.summary = "stBSL=" + it.getKey() + " " + issue.summary

                        issueLinkManager.createIssueLink(issue.getId(), it.getId(), new Long(12000), null, authenticationContext.getLoggedInUser())
                    }
                }
            }
    }
}

should do it.

@Jonny Carter  Is there anyway I can link issue A with issue B here?

I'm a bit confused. Linking the cloned issue with its original is exactly what the Clone Issue and Link post function already does. Am I misunderstanding which issues are A & B or something??

@Jonny Carter When creating an issue A, with  Clone Issue and Link post function, it will generate a clone issue, issue B. But A and B are not linked.

From the online doc I think what that mean is if A is set link to C, then new created clone issue B will also not link to C. But in my case, I want A link to its clone B. Anyway I can do that? If I am wrong could you show me how to link A & B?

@Jonny Carter How to get Issue A id and issue B's id, I did try use 

sourceIssue.getKey(),sourceIssue.key

but they all return null 

@QQQ - You should be able to specify a link type and direction. From those docs:

Specify also the link type and direction…​ this will normally be Clones, but doesn’t have to be.

As for getting the issue keys in the additional code, sourceIssue.key should work, but it may not work unless you put it inside a doAfterCreate closure. See https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_after_create_actions. 

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 yesterday in Jira Service Desk

Wy are we still using email for Service Desk workflows?

...attest to the experience of an urgent approval that gets lost in the boss’s inbox and requires that special “Please Approve” email or text message. In an age where we have distributed teams...

114 views 0 2
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