Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

How to set a customfield value of a linked ticket using script runner

I have a parent ticket and a linked ticket.now I want to update the linked ticket's one of the customfield..can someone help me wit this. I am trying to write script under post function

1 answer

1 accepted

1 vote
Answer accepted
Antoine Berry Community Leader Apr 04, 2019

Hi,

Keep in mind you need criterias to define which linked issue to update. This snippet will get all the linked issues (inward and outward) of the current issue. I added some logs so you can decide which criteria to apply, and an example of updating the custom field value with current issue value or parent issue value.

Do not forget to update custom field id.

import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;


def customFieldManager = ComponentAccessor.getCustomFieldManager()
int cfId = 11002
def cf = customFieldManager.getCustomFieldObject(cfId)
def cfValue = issue.getCustomFieldValue(cf)
def cfParentValue = issue.getParentObject().getCustomFieldValue(cf)

List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
def linkedIssue = issueLink.getDestinationObject()
def linkedIssueTypeName = issueLink.getIssueLinkType().getName()
def linkedIssueCfValue = linkedIssue.getCustomFieldValue(cf)

log.debug("issueLink : " + issueLink.getIssueLinkType().getName())
log.debug("linkedIssue : " + linkedIssue)
log.debug("linked issue type : " + linkedIssue.getIssueType().getName())

if (linkedIssueTypeName == "Cloners"){
//Updates with parent issue value
cf.updateValue(null, linkedIssue, new ModifiedValue(linkedIssueCfValue, cfParentValue), new DefaultIssueChangeHolder())
// Updates with current issue value
cf.updateValue(null, linkedIssue, new ModifiedValue(linkedIssueCfValue, cfValue), new DefaultIssueChangeHolder())
}
}

List<IssueLink> allInIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
for (Iterator<IssueLink> inIterator = allInIssueLink.iterator(); inIterator.hasNext();) {
IssueLink issueLink = (IssueLink) inIterator.next();
def linkedIssue = issueLink.getSourceObject()
def linkedIssueTypeName = issueLink.getIssueLinkType().getName()

log.debug("issueLink : " + issueLink.getIssueLinkType().getName())
log.debug("linkedIssue : " + linkedIssue)
}

 Antoine

Hi @Antoine Berry 

I am trying to update child value from parent ticket itself. So in your code will cf.updateValue update the customfield in child ticket? I think it will check in current issue whereas the field exists only in linked ticket not in parent ticket

 

I am trying to set a customfield value for a date field in the linked ticket as below

IssueLinkManager linkManager = ComponentAccessor.getIssueLinkManager();
List<IssueLink> issueLinks = linkManager.getOutwardLinks(issue.getId());

for(IssueLink link : issueLinks) {
log.debug("link name " +link.getDestinationObject().getStatus().getName())

if (link.getIssueLinkType().getName().equals("XXX)")){

Issue duplicateTicket = link.getDestinationObject();


duplicateTicket.setCustomFieldValue(plannedregDate,issue.getCustomFieldValue(plannedrcdate))

 

Is there anything I am missing

Antoine Berry Community Leader Apr 04, 2019

I think you are confusing issue links and parent to child links. They are different : 

image.pngUpdating a sub-task is easier : 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
int cfId = 11002
def cf = customFieldManager.getCustomFieldObject(cfId)
def cfValue = issue.getCustomFieldValue(cf)

for (Issue subtask:issue.getSubTaskObjects()) {
log.debug("sub task key : : " + subtask.getKey())
log.debug("sub task issue type : : " + subtask.getIssueLinkType().getName())
def subTaskCfValue = subtask.getCustomFieldValue(cf)
cf.updateValue(null, subtask, new ModifiedValue(subTaskCfValue, cfValue), new DefaultIssueChangeHolder())
log.debug("updating customfield " + cf + " of issue " + subtask + " with value " + cfValue)
}

Antoine 

I am working with issue links not the subtask.

So in your code, cf.updateValue().. this customfield doesnt exist on the ticket from which i am writing the code.

So will this method go and update the value in the linked ticket? I was assuming cf.UpdateValue will update in the current ticket

Antoine Berry Community Leader Apr 04, 2019
cf.updateValue(null, issue, new ModifiedValue(oldValue, newValue), new DefaultIssueChangeHolder())

updates the custom field value on the issue "issue" from "oldValue" to "newValue". Now replace these with whatever you want.

Great, it worked ..thank you :)

Like Antoine Berry likes this

Hey @Antoine Berry

How are u?


Along these lines, I can do something like this:

Issue parent has "N" issue child linked to it.

In the issue child I have a field X that registers hours (It's a sigle line text field) and in the issue parent I have a field that needs to receive the result of the sum of hours pointed in the issue child field.

Ex: Field Issue parent____ ?h

Field Issue child____2h
Field Issue child____2h
Field Issue child____2h
Issue child time result = 6h <--- This result must appear there in the parent issue.

Can you help me?

Like Antoine Berry likes this
Antoine Berry Community Leader Dec 01, 2021

Hello @Raphael Henrique Fernandes Lopes ,

Could you please create a new question (you can ping me on it) ? This way everyone will be able to help you.

This is definitely possible, however I would probably advise to use a numeric field instead of a text field.

Suggest an answer

Log in or Sign up to answer
TAGS

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