I am trying to set single select custom field in post-fuction, but I don't have much luck.
We are using still old Scriptrunner 6.55 and the examples on the web don't exactly work. Documentation does not have an example for single select.
I currently use this code to update a date field, which works, but I am not able to figure out how to update single select field.
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// a date time field
def dateCf = customFieldManager.getCustomFieldObject("customfield_21303") // Date time fields require a Timestamp
issue.setCustomFieldValue(dateCf, new Timestamp((new Date()).time))
Anyone can help?
Hi there!
Not sure what in the documentaion you have tried already, but ths should help you on your way:
import com.atlassian.jira.component.ComponentAccessor
def issue = ComponentAccessor.getIssueManager().getIssueObject("SOME-ISSUE_KEY")
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(14810L)
def optionsManager = ComponentAccessor.optionsManager
def fieldConfig = customField.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig)
def option = options.find { it.value == "Operations" }
issue.setCustomFieldValue(customField, option)
What it does:
The should still be some logic added to actually update the issue (with the issueManager).
Hope this helps!
Regards,
Jeroen
I see that you're on an old version of ScriptRunner, and you have an answer compatible with that version.
In future if you can upgrade, I think HAPI would be very beneficial, it drastically simplifies this kind of thing.
To achieve the same result in a post function with HAPI, you'd use code like this:
issue.set {
setCustomFieldValue(21303L, 'Operations') // By ID
setCustomFieldValue('FieldName', 'Operations') // By name
}
https://www.scriptrunnerhq.com/hapi
https://library.adaptavist.com/entity/set-custom-field-value
Cheers!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Reece Lander _ScriptRunner - The Adaptavist Group_ ,
From what version can we use this? This is awesome!
Regards,
Jeroen
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HAPI is available in ScriptRunner for Jira 7.11.0 and above :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here are some more examples for updating custom fields with HAPI:
https://library.adaptavist.com/entity/basics-updating-customfields
The syntax for setCustomFieldValue is identical in a post function, you just need to remember to use issue.set { } not issue.update { } in a post function, the script editor in app will warn you if you use the wrong one.
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.