Updating Custom Field based on Linked Issue Edited

Hey all! 

New to scriptrunner but really excited at it's possibilities. I am using JIRA Cloud and Scriptrunner version- 1.1.21-AC 

I want to copy a custom field from a linked issue. 

ie- Issue 1 has custom field "Mobile Number", Issue 2 has empty "Mobile Number" field.

When 1 is linked to 2, Issue 2's mobile number is replaced with issue 1's. 

Is this possible with ScriptRunner for JIRA Cloud? 

I hope that makes sense.

2 answers

1 accepted

This widget could not be displayed.

Hi Ryan,

the functionality you described can be achieved with Scriptrunner for JIRA Cloud. You will have to create a Script listener for an event IssueLink Created as described in Script Listeners documentation. As you can see from the example you can access the source and destination issue and this way ready the custom fields, in your case phone number. Once you have the desired value you can use similar script to this example to update the custom field].

Hope it helped, if you need further assistance let me know. 

Regards,

Martin, Adaptavist 

Hey Martin, 

Scriptrunner got put on the back burner for other projects, but we're back! Forgive my ignorance, extremely new to this. 

I was using the example you linked in your comment but it's giving me "variable [issue] is undeclared", what's the proper way of declaring "issue"?  

And how do I pull custom fields from api? Is it via the ID of the custom field or the user friendly name? ie 123456 vs "Mobile Number"

 

Thanks! 

Hi Ryan,

issue is a parameter passed to your Script Listener script in Script Context. The example i referred to describes how to get the value: issue.fields[input1CfId] Use logger to print the values once testing.

Are you using Script Console for your script or Script Listener code once you got the error. Also don't worry about the linting errors like 'variable [issue] is undeclared'. The linter is not perfect and sometimes can not determine the class and properties of the variables. Just try to run it.

Let me know if you need more help and please attach your code.

Regards,

Martin

Hey Martin, 

Thanks for your help. Made some head way. I'm trying to access the highlighted field within inwardIssue. Is there a way to deserialize the .json?

Screen Shot 2017-12-20 at 6.28.58 PM.png

Hello Ryan,

issue is a hashmap, issue.fields.issueLinks to access an array of links, iterate over them and find the one for inwardIssue then just access the key. Also there may be more inwardIssues in the issueLinks array. 

Btw this question is not really Scriptrunner for JIRA Cloud related but Groovy programming language related as you are trying to access a filed in a hashmap. 

But back to your original question, i thought you are going to create Script Listener on issuelink created event and there you have issueLink.sourceIssueId and issueLink.destinationIssueId you can use as in an example i sent you.

Please, next time attach your code so it is clear to me what are you trying to achieve.

Regards,

Martin

Hey Martin,

Sorry, if that was confusing. The goal is still the same as in the original post.  So I tried just copying the example for issuelink created, but none of the logger.info statements show up in the log files so I couldn't validate if what I was doing was working as intended. Maybe I'm not testing it correctly? I have been removing existing issue links and adding an issue link to test the out of the box script. 

The below snippet is running as current user, on IssueLink Created

def sourceIssueResponse = get("/rest/api/2/issue/${issueLink.sourceIssueId}").asObject(Map)
assert sourceIssueResponse.status == 200
def sourceIssueKey = sourceIssueResponse.body.key
def sourceIssueSummary = sourceIssueResponse.body.fields.summary

def destinationIssueResponse = get("/rest/api/2/issue/${issueLink.destinationIssueId}").asObject(Map)
assert destinationIssueResponse.status == 200
def destinationIssueKey = destinationIssueResponse.body.key
def destinationIssueSummary = destinationIssueResponse.body.fields.summary

logger.info("A link has been created: Issue {} issue {}", sourceIssueKey, destinationIssueKey)

 So I tried writing something that fired on Issue Updated. But that event doesn't support issueLink

Hello Ryan,

i have tested the code you provided and all works fine for me. I have created Script listener like thisScreen Shot 2017-12-22 at 11.12.05.png afterwards, i have created a issuelink in one of my project. As you can see the History is showing a successful execution 4 mins ago. Clicking on the green icon shows the execution details and logs, where i can see the logs. 

Btw. in the same was as you are accessing summary you can access your phone number custom field. (You need to figure out the name of that custom field by printing out the

sourceIssueResponse.body.fields

and in case it does not exist you need to update the issue as described here

Regards,

Martin

Hmmm... got it. Why does the listener not work when it's limited to specific projects?

I was able to pull the mobile number by hardcoding it as below. How can I use mobileNum instead? 

def mobileNum = 'customfield_10208'
def destinationMobileNumber = destinationIssueResponse.body.fields.customfield_10208

Hello Ryan,

i am going to check if there is a problem with restricting the listener for a specific project. 

Do you mean to use the mobileNum variable to access the field in a Map? That is actually groovy question not Scriptrunner for JIRA Cloud question. But try this:

def destinationMobileNumber = destinationIssueRersponse.body.fields[mobileNum]

Regards,

Martin

This widget could not be displayed.

Hello Ryan,

  Recently i used this groovy script Copy Custom field values from Epic to linked issue ie Story, Bug, Chore issue types. May be it will be helpful to you if you adjust some code.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issue = issue as MutableIssue


["Capitalizable?"].each \{ cfname ->
 def cf = customFieldManager.getCustomFieldObjects(issue).find \{it.name == cfname}
 def epicCf = customFieldManager.getCustomFieldObjects(issue).find \{it.name == 'Epic Link'}
 if (!cf || !epicCf) \{return}
 
 def epic = issue.getCustomFieldValue(epicCf) as Issue
 if (!epic) \{return}
 
 def cfValue = cf.getValue(epic)
 
 issue.setCustomFieldValue(cf, cfValue)
}

Thanks Laxmi! This looks like it's for JIRA server. We're on cloud :(

 

But how did you learn ScriptRunner? 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

524 views 3 19
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