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
}
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you, Paul. I will try this out in a couple days and let you know how it works. Thanks again!
Jeff
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
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.