Check custom field value in a groovy script

Ryan Deyer February 22, 2017

I'm using the code below in a ScriptRunner custom listener.  Bolded in the if statement is a confirmation that the Priority is set to Critical.  That all works so far.  If I wanted to replace the Priority check with a custom field instead (i.e., check if custom field 'Name' is equal to 'Ryan'), how would the code change?  I can't seem to get it to work.

 

import groovy.json.JsonBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "priority"}
if (change && issue.priority.name == "Critical") {

// code to ping HipChat room

}

1 answer

1 accepted

10 votes
Answer accepted
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2017

You will have to fetch and check for custom field value.

import com.atlassian.jira.component.ComponentAccessor;
 
def customField =  ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10259");// here replace the ID with ID of your custom field.
 
def value = (String)issue.getCustomFieldValue(customField);	
 
//and now simply check
if(value == "Ryan")

Also, do put the null checks as well.

Ryan Deyer February 23, 2017

Thank you so much. That worked!

Sangram Darole June 13, 2018

i was trying to write a script which finds a Expiry Date ( Custom field Date Picker ) which is after 8 days.  and then send email to reporter and assignee. I am using post function and custom email scripted function... Thanks in advance

Mehran Namdar September 27, 2019

thanks man it's worked for me

Jeff Hayes August 5, 2021

On this topic, how do you go about finding out what the custom field ID is?  I have a custom field called "Environment" which is a list of six, single-select, drop down choices, but I'm not sure how to find out what Jira thinks the ID's of each of those choices are.

 

Thanks,
Jeff

Paul Mata August 18, 2021

@Jeff Hayes one way is to do a search for issues (Advanced mode) and as you type the field name into the query, it will show you the custom field id. The ID is for the field, not the responses. The responses are what you would check for in that if statement at the end of the script.

customfield.png

Like Jeff Hayes likes this
Jeff Hayes August 19, 2021

I'm actually trying to set the optionID to a specific value.  I know the custom field ID now thanks to your advice above, but I'm not sure how to go about setting the optionID.

This is the code that someone helped me write, but it assumed a text field.   My custom fields's ID is 10300 and I am trying to set it to "Prod":

def issue = issueManager.getIssueObject(indexIssue.id)
def EnvField = customFieldManager.getCustomFieldObjects(issue).findByName(EnvFieldName)
mylog.debug "EnvField is $EnvField"
assert EnvField : "Could not find custom field with name $EnvFieldName for $issue.key"
mylog.debug "EnvField current Value for $issue is ${issue.getCustomFieldValue(EnvField)} setting it to 'Prod'"
issue.setCustomFieldValue(EnvField, 'Prod') // <-- this will only work if EnvField is a text field. If it's a select, you'll need to find the OptionId that correspond to the label "Prod"

 Can you help me modify this so that it works since it's not a text field?  

 

Thanks,
Jeff

Paul Mata September 8, 2021

@Jeff Hayes try this - I tested this script in my Jira instance and it updated the value of the field. This is modified from a script in the Adaptavist library.

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager

@ShortTextInput(description = "The key of the issue to be updated", label = "Issue Key")
String issueKey

//the name of the custom field
final customFieldName = 'Documentation Update Rqmt' // select list name

// the value of the new option to set
final newValue = 'Not Needed' // this is the value you want to select

def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey)
assert issue: "Could not find issue with key $issueKey"

def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
assert customField: "Could not find custom field with name $customFieldName"

def availableOptions = ComponentAccessor.optionsManager.getOptions(customField.getRelevantConfig(issue))
def optionToSet = availableOptions.find { it.value == newValue }
assert optionToSet: "Could not find option with value $newValue. Available options are ${availableOptions*.value.join(",")}"

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())

 

Line 5 and the associated import are for using this script in the scriptrunner console which I normally do for testing - change it to the following for your script:

def issue = issueManager.getIssueObject(indexIssue.id)

Hopefully that helps. You should just need to replace the field name and value and it is ready to run. You should also be able to modify this script to set the value based on another variable. For example, if field A is not empty, set Environment field to "Prod".

Jeff Hayes September 9, 2021

Thank you, Paul.  I will try this out in a couple days and let you know how it works.  Thanks again!

Jeff

Pratibha Tambakad March 14, 2023

Hey Paul,

I have the similar requirement where I need to update the field option in nearly 700 project contexts. I am not updating on particular issue, I need to update the value in context.

Please let me know if you have any solution around this,

Suggest an answer

Log in or Sign up to answer