Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script to read custom field using script runner not working

Jeff Hayes
Contributor
March 17, 2021

I've attempted to use the code listed in this closed thread:

https://community.atlassian.com/t5/Answers-Developer-Questions/Looking-for-a-script-to-fill-customfield-multiple-checkboxes-in/qaq-p/543685

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&lt;Option&gt;) 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

1 answer

1 accepted

0 votes
Answer accepted
Ravi Sagar _Sparxsys_
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.
March 17, 2021

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

Jeff Hayes
Contributor
March 17, 2021

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

Ravi Sagar _Sparxsys_
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.
March 17, 2021

Did you try the modified code? I believe it should give you a list like this - [option1, option2, option3]

Jeff Hayes
Contributor
March 17, 2021

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

Ravi Sagar _Sparxsys_
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.
March 17, 2021

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

Like Jeff Hayes likes this
Jeff Hayes
Contributor
March 17, 2021

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

Ravi Sagar _Sparxsys_
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.
March 17, 2021

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

Like Jeff Hayes likes this
Jeff Hayes
Contributor
March 17, 2021

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

}

 

     

     

Ravi Sagar _Sparxsys_
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.
March 17, 2021

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

Like Jeff Hayes likes this
Jeff Hayes
Contributor
March 18, 2021

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

Suggest an answer

Log in or Sign up to answer