How to set issue links through groovy in script postfunction?

miller j
Contributor
January 27, 2018

Hello,

 I am trying to set the issue links though post functions using groovy. I would like to create the links based on a custom field value from the current issue to the new issue.

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('<customfield_id>')

def customFieldValue = issue.getCustomFieldValue(customField)

def newIssue = 'abc-123'

if(customFieldValue  = 1){

Link blocks

}

else {

Link clones

}

 

Thanks a mill.

 

1 answer

1 accepted

2 votes
Answer accepted
Ivan Tovbin
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.
January 27, 2018

Here's how you do it.

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
linkMgr.createIssueLink(11111, 22222, 33333, 1, currentUser)

/*where 11111, 22222 and 33333 are source issue id, destination issue id and issue link type id respectively */
/*currentUser is the link creator, so feel free to change it to any other Application User object*/
Qi Jiang
Contributor
April 10, 2018

@Ivan Tovbin

Hi Ivan,

I did try to run this in script console instead of post function, but got some error on

linkMgr.createIssueLink

 can't find match method

Ivan Tovbin
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.
April 10, 2018

It's the usual thing with Scriptrunner's Static Type Checking not always being accurate. 

In this very case you can either simply ignore this error - the code will work just fine. Or you can declare argument types like so, to make the error go away:

linkMgr.createIssueLink((Long)11111, (Long)22222, (Long)33333, (Long)1, currentUser)
Dante Labate September 10, 2018

@Ivan Tovbin

could you tell me how I remove link?

Suggest an answer

Log in or Sign up to answer