I've attempted to use the code listed in this closed thread:
Modified as such to use the actual name of my custom field:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Value Statement Impact")
StringBuilder stringBuilder = new StringBuilder()
((List<Option>) issue.getCustomFieldValue(customField)).each {
stringBuilder.append(it.getValue()).append(", ")
}
return stringBuilder.toString()
However, I am getting the following error when doing so:
The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script5.groovy: 9: expecting ')', found 'issue' @ line 9, column 24. ((List<Option>) issue.getCustomFieldValue(customField)).each { ^ 1 error </pre>.
Can someone comment as to where I may have made an error in my copy/paste of the code?
Thanks,
Jeff
Hi @Jeff Hayes
What is the type of your custom field?
I modified your code slightly.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Value Statement Impact")
return issue.getCustomFieldValue(customField[0])
This should get you the value in the custom field but based on your custom field type you need to write a bit more code to get the value in a proper format.
Ravi
Ravi,
Thanks. It's a multiple checkbox field with 4 choices. Based on the previous thread, my understanding was that the code would aggregate all the values into an array or something of that nature.
Thanks,
Jeff
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you try the modified code? I believe it should give you a list like this - [option1, option2, option3]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It errors out, as well with:
groovy.lang.MissingPropertyException: No such property: issue for class: Script7 at Script7.run(Script7.groovy:7)
That's the return line that it's erroring out on.
Is there a way for me to verify that I am referring to my custom field in the proper way? I mean, I know that's the name of it in terms of how I created it in Jira, but perhaps it's referred to in a different fashion when you use it in scriptrunner?
Jeff
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I missed the issue object.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Value Statement Impact")
IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("ISSUEKEY-1")
return issue?.getCustomFieldValue(customField[0])
Try this code, it should work. Make sure to use the correct ISSUEKEY
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, Ravi. I apologize for my lack of familiarity with this programming language, but what is an "ISSUEKEY" and where would I find info to know if it was the "correct" one?
Thanks,
Jeff
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Jeff Hayes
The custom field value that you want to fetch is for a specific issue. Each issue in Jira has a unique key. For instance issues in project with key PRJ will be like PRJ-1, PRJ-2, PRJ-3 etc
In the code I shared replace ISSUEKEY-1 with the relevant issue key. This key is displayed in the address bar in your browser when you are looking at the issue.
This code will work fine in the ScriptRunner Console but will need modification when used in a post function or listener, basically comment out this line.
MutableIssue issue = im.getIssueObject("ISSUEKEY-1")
I hope it helps.
Ravi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Perfect! Now I understand! Thanks! This all leads to a bigger part of this script. What I'd like to do is assign a numeric value (weight) based on which of the checkboxes are checked and then assign the sum of those weights into another custom field that I've created. The pseudo code for this in my mind is essentially:
weight = 0
If customfield1 is not NULL
{
if customField1 contains "Revenue"
{
weight += 4
}
if customField1 contains "Client"
{
weight += 3
}
if customField1 contains "Member"
{
weight += 2
}
if customField1 contains "User"
{
weight += 1
}
customField2 = weight
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Jeff Hayes
That is also possible. I suggest you take a look at our script library where we have plenty of examples. For instance this script will give you an idea about how to update a custom field value.
I hope it helps.
Ravi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks again, Ravi! I will be checking out all of the scripts on the site to see if I can piece together what I need!
Jeff
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.