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.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Ryan,
i have tested the code you provided and all works fine for me. I have created Script listener like this 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Laxmi! This looks like it's for JIRA server. We're on cloud :(
But how did you learn ScriptRunner?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.