Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Set linked issue

Hello!

I want to set a linked issue with groovy post function.

 

My current code didn't work... why ? Thanks!

def newFeatureIssue = issueManager.getIssueObject(temp) // Epic issue key here
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def featureLink = customFieldManager.getCustomFieldObjectByName("Parent Feature")
featureLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(featureLink), newFeatureIssue),new DefaultIssueChangeHolder())
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

1 answer

1 accepted

0 votes
Answer accepted

Hello,

if you want to set a link then your code should look like this;

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

You can find more details here:

https://community.atlassian.com/t5/Jira-questions/How-to-set-issue-links-through-groovy-in-script-postfunction/qaq-p/713709

Thanks @Alexey Matveev _cPrime_ ,

Do you know how can I take the issue link type of inward ? I find the id of the link but how can I refer to the inward specifically ?

 

Thank you!

Hello,

Each link has the outward and inward descriptions. Do you mean that you want to know the id of the inward description? If so, you do not need it. 

When you create a link between issues using createIssueLink, the first variable is the source issue(outward) and the second variable is the destination issue (inward).

Thanks !! you right :) @Alexey Matveev _cPrime_

The only problem is that the link is added and not switch.. How can I delete the previous one ??

It would be something like this:

    def linkMgr = ComponentAccessor.getIssueLinkManager();
    def issueLink = linkMgr.getIssueLink(sourceIssue.getId(), destinationIssue.getId(), linkTypeID);
   if (issueLink != null) {
      linkMgr.removeIssueLink(issueLink, user);
   }

The IssueLinkManager class is responsible for handling links. You can have a look at all available methods here:

https://docs.atlassian.com/software/jira/docs/api/7.1.4/com/atlassian/jira/issue/link/IssueLinkManager.html 

@Alexey Matveev _cPrime_ Put this code before or after the 

createIssueLink

?

 

When I put it after, it deleted the link I just added, I need to delete the previous one. 

You should put it before the createIssueLink

@Alexey Matveev _cPrime_

Hi, I did it like this, and it worked in the console..

I copy it to post function.. not working, why is that ?

 

 

//remove the new feature
issueLinkManager.getInwardLinks(issue.id).each {IssueLink iLink ->
if(iLink.getLinkTypeId() == 10402){ //Feature Epic

Issue linkedIssue = iLink.sourceObject
issueLinkManager.removeIssueLink(iLink, user);
}
}
def userManager = ComponentAccessor.userManager
//Add the previous feature
try
{
issueLinkManager.createIssueLink(PrevFeature.getId() as Long,issue.getId() as Long, 10402L as Long, 1 as Long, user);
//issue.store();
//ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
}
catch (Exception e) {
throw e
}

Is this post function in the create issue tranistion? If so, is it last in the list of post functions? What are the errors? What does not work?

@Alexey Matveev _cPrime_ , It is a post function of 'change parent' , and I put it first inside epic workflow.

There are no error, the link didn't change.. I try to take the previous one, and switch the new one with the previous..

 

This is my full code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.version.Version
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean 
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import com.atlassian.jira.project.version.VersionManager 
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.issue.link.LinkCollectionImpl;
import com.atlassian.jira.issue.link.IssueLink;

IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)

//def issueManager = ComponentAccessor.getIssueManager();
//test
//Issue issue = issueManager.getIssueObject("WBL-19638");
// your actions if the field has changed

//Get fix versions of issue
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = issue.getFixVersions();
//Get temp with fix version
def tempVersion=fixVersions[0];

//Get Epic Link Key
def feature = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent Feature");
def featureKey = issue.getCustomFieldValue(feature);
Issue featureIssue = issueManager.getIssueObject(featureKey.toString());
log.warn featureKey;

//Internal feature checkbox ?
def internal = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Internal Feature");
//return internal;
def selectedValues = internal.getValue(featureIssue);

if(selectedValues.toString()!='[Yes]')
{

//Get fix version of Epic
Collection<Version>fixVersionsFeature = new ArrayList<Version>();
fixVersionsFeature=featureIssue.getFixVersions();
def tempVersion2=fixVersionsFeature[0];
//If tempVersion == fixVersionsEpic
if(tempVersion.toString()==tempVersion2.toString())
{
return;
}
//if temp != fixVersionsEpic
if(tempVersion.toString()!=tempVersion2.toString())
{
//error message
UserMessageUtil.error("Failed. Fix version of Epic should be like the Feature's fix version");
//Get list and size of the previous versions
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
List<ChangeItemBean> changeList = changeHistoryManager.getChangeItemsForField(issue, "Parent Feature")
def size = changeList.size();
def temp= changeList[size-1].getFromString();
//Set the previous feature
Issue PrevFeature = issueManager.getIssueObject(temp.toString());
//Set the previous feature
def userManager = ComponentAccessor.userManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def issueLinkManager = ComponentAccessor.getIssueLinkManager();

//remove the new feature
issueLinkManager.getInwardLinks(issue.id).each {IssueLink iLink ->
if(iLink.getLinkTypeId() == 10402L){ //Feature Epic

Issue linkedIssue = iLink.sourceObject 
issueLinkManager.removeIssueLink(iLink, user);
}
} 

//Add the previous feature
try
{
issueLinkManager.createIssueLink(PrevFeature.getId() as Long,issue.getId() as Long, 10402L as Long, 1 as Long, user); 
//issue.store();
//ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
}
catch (Exception e) {
throw e
}

}
}
else
{
return;
}

 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

Share your Confluence Cloud experience for a chance to win $500!

Take our 5-minute survey to win a $500 Visa gift card! Are you currently using Confluence Cloud? We want to hear from you! Fill out this quick survey about your Confluence Cloud experience so we ...

238 views 5 13
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you