Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script to read custom field using script runner not working

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

0 votes

Hi @Iceblade119 

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

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

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

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 Iceblade119 likes this

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

Hi @Iceblade119 

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 Iceblade119 likes this

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

}

 

     

     

Hi @Iceblade119 

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 Iceblade119 likes this

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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you