script runner return custom field value from different issue

Junaid Shah
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.
November 26, 2015

Hi,

I am trying to return a custom field value from a different issue, I have the Issue ID stored in a Custom Field on the current issue.

I am running JIRA 5.2

 

I am using the Script Runner Plugin to create a subtask, when creating the subtask I  want to retrieve the value of a field, from the issue stored in my customfield.

 

e.g.

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

//Issue that I would like to access to get the field value
def TargetIssueID = cfValues['Target Issue']

//Custom Field that I am trying to retrieve the value from:

CustomField request_type = customFieldManager.getCustomFieldObjectByName('Supported Service')

//Here the code will be to retrieve the value of "Supported Service" from the "Target Issue"


//Then Setting the field in the subtask
issue.setCustomFieldValue(request_type, target_Issue_request_type_value)

Any help greatly appreciated..

6 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Junaid Shah
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.
November 29, 2015

I have made it work using

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager() 

def cf = cfValues['Issue ID Link'] 

def issueManager = ComponentAccessor.getIssueManager() 

 //cf being field that contains the Issue ID 

def targetIssue = issueManager.getIssueObject(cf) 

CustomField request_type = customFieldManager.getCustomFieldObjectByName('Custom Field 1 to Set')

def request_type_value = targetIssue.getCustomFieldValue(request_type)

// set the value of Custom Field 1 in the current issue's customField

issue.setCustomFieldValue(request_type,request_type_value)

Thanks to Thanos.

1 vote
Junaid Shah
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.
November 27, 2015

Also the fields that are being copied over, they're not from the Linked Issue or Target Issue are they? or Parent.. as I need them from a Targeted Issue not parent..

1 vote
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 26, 2015

First thing is that you need to think about where this is running.  You've not said if it's a post-function, listener or something else, and that matters because the context of "which issue" is critical here.

Your first line that gets the targetIssueID is ok, but as it's going to contain a string or an array, you're going to need to convert it to an issue object before it's of any use.

request_type is fine, it's a custom field handle

The last line is where the context matters.  What issue?  Where does that "issue" object come from?  If you are doing this in a post-function or listener, then that issue object will be the current issue that the user has just transitioned. 

Your code is broadly correct, but you need to think through the process and context.

 

Junaid Shah
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.
November 27, 2015

It is a scripted Post Function - using the Scriptrunne plugin "Create a Subtask"

Junaid Shah
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.
November 27, 2015

The issue itself is in a different project, issue.setCustomFieldValue is being used at the end to SET the field in the NEW Subtask that is being created, using the value that is retrieved from the Target Issue.

Junaid Shah
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.
November 27, 2015

As this is a Scripted Post Function - "Create a Subtask" my main task is to set the field values as the new subtask is created. I can do Parents to Subtask but I need to do Linked Issue to Subtask. The Custom Field Target Issue will only contain a single Issue ID per transition so that is fine. I just need a method, or a piece of code which I can use to access the Target Issue and retrieve Custom Field Values, as you would from Parent. e.g. For Parent I use: def request_type_value = transientVars["issue"].getCustomFieldValue(request_type) But I cannot use above to instead go to a specific Issue ID..

Junaid Shah
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.
November 27, 2015

I have tried to use def targetIssue = IssueManager.getIssueObject(targetIssueID) But it fails and does not create a subtask.

0 votes
Junaid Shah
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.
November 27, 2015

Hi Jeff, Thanks I appreciat your help, I had tried your code yesterday but failed to make it work, Your code, would you need to save the script and set it as a file path? or where would you use it then? just to clarify.

0 votes
Jeff Louwerse
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.
November 27, 2015

Sy, I gave you the code yesterday to create a subtask and copy all the fields. Instead of using the built in "crate subtask" use my code rather than trying to do it in two different scripts. I am not sure why you would want to do this in two different scripts? and if you don't want to copy all the fields, just create a list of fields you want to copy and loop over them.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events