I have a few field in my issue when it is edited. I want to get the values of the fields and from the answer fields, only the values of selected one when the update button is pressed. I have currently following script which produces a huge json and contains a lot of irrelevant information. Could someone help me with this? thanks
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.sql.Timestamp
import com.atlassian.jira.config.util.JiraHome
import org.apache.commons.lang3.StringEscapeUtils
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import groovy.json.JsonSlurper
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue_obj = issueManager.getIssueObject("TM-197") // the tm_confirmation issue number, replace with the real id
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_12345") //Custom Field Value - object confirmation, replace with the real id
def cFieldValue = issue_obj.getCustomFieldValue(cField)
cFieldValue // Custom Field Json
In the below picutre, i have questions and answers. I want to get the value of the question and selected answer when the update button is pressed.. I am totally new to Jira so facing a number of issues. Is there any guide to learn the groovy script which I can run in the script runner console? thanks
any help would be really appreciated. thanks
Your code is broadly correct for a generic case of "get the current value of a custom field", the object cFieldValue will, at the end of the script, be holding the value from customfield_12345 for issue TM-197. It's getting a single field value.
There are more things to think about next
If you want the content of many different fields, you'll need to repeat the code for each field, adjusting the "customfield_12345" to pick up the id of each different field, and probably putting each one in a different variable
Fields hold different types of data, and your code that works with them needs to understand the data. You can always say what cFieldValue will contain a particular type of data, but it depends on the type of field it is. If, for example, it's short text, cFieldValue will hold a short string, for a date or date time, it will be holding a timestamp object, if it's a number, you'll get a long, if it's a multi-select or checkbox, you'll see an array of options, and so-on - all different types of object, handled differently.
So, the next question is "what type of field are you trying to read"? Simple way to answer that is to go to the list of custom fields, find it, and look underneath its name.
thanks for guiding me about that. is there any source where i can learn groovy for script runner? thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
https://scriptrunner.adaptavist.com/5.5.8/jira/quickstart.html is not a bad place to start, and I tend to steal liberally from https://library.adaptavist.com/ (even the scripts I wrote that made it there!)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.