Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

scriptrunner post-function set single select custom field

Jiri
Contributor
March 5, 2023

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?

2 answers

1 accepted

3 votes
Answer accepted
Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 5, 2023

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:

  • Get an issue
  • Get the select CF with ID 14810 (change this of course)
  • Get the fieldconfig of the select CF relevant to the issue
  • In the fieldconfig find the Option with name "Operations" (Find your value here)
  • Set the custom field on the issue 

The should still be some logic added to actually update the issue (with the issueManager).

Hope this helps!

Regards,

Jeroen

Jiri
Contributor
March 6, 2023

Works like a charm. I only commented out the second line of the code as the post-function can run it on any issue.

Thank you!

4 votes
Reece Lander _ScriptRunner - The Adaptavist Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 6, 2023

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!

Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 6, 2023

Hi @Reece Lander _ScriptRunner - The Adaptavist Group_ ,

From what version can we use this? This is awesome!

 

Regards,

Jeroen

Reece Lander _ScriptRunner - The Adaptavist Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 6, 2023

HAPI is available in ScriptRunner for Jira 7.11.0 and above :)

Reece Lander _ScriptRunner - The Adaptavist Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 6, 2023

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.

Suggest an answer

Log in or Sign up to answer