Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Groovy script to update a custom field

Dean Stearn December 30, 2018

Hi

I am trying (with no success at all for the last few hours!!) to create what I think is a simple post function groovy script that will update a custom field as a Jira Service Desk ticket transitions from one state to another.

For example, a ticket is raised and has the state 'Waiting Approval'

When approved, the ticket transitions from 'Waiting Approval' to 'New'

During this transition, I am trying to set the value of a custom field called 'Escalate to' to a value of 'Support'

The custom field is a Single List (single choice) type field. The value 'Support' is an available option.

I have been trawling the web for suitable answers / examples but have not been able to adapt them successfully.

If anyone can help, I would appreciate it.

Secondly, what are the best sites to become more familiar with Groovy.

Thank you

Dean

 

2 answers

1 accepted

3 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.
December 31, 2018

Hello @Dean Stearn

For a code sample, as to how you can set a value in a custom field of type Select, please see the below link. There are plenty of such examples on this forum

https://community.atlassian.com/t5/Answers-Developer-Questions/How-do-I-programmatically-set-values-for-custom-fields-of-type/qaq-p/485784

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.
December 31, 2018
Dean Stearn December 31, 2018

Thank you @Tarun Sapra

I have copied the example and now have the following code:
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def formField = getFieldByName("Escalate to")

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(formField.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionToSelect = options.find {
it.value == "System Support"
}
formField.setFormValue(optionToSelect.optionId

 

and the results I now get after moving a service desk ticket from 'Waiting Approval' to 'New'

Time (on server): Mon Dec 31 2018 10:48:27 GMT+0000 (Greenwich Mean Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-12-31 10:48:27,801 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-12-31 10:48:27,802 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SD-72, actionId: 11, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getFieldIdByName() on null object
at com.onresolve.jira.groovy.user.FieldBehaviours.getFieldByName(FieldBehaviours.groovy:53)
at Script274.run(Script274.groovy:7)

Do you know what is causing my issue?

Kind regards
Dean

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.
December 31, 2018

Hello @Dean Stearn

Since you are trying to update the custom field in the post-function thus please try the following code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def customFieldManager = ComponentAccessor.customFieldManager
def escalate = customFieldManager.getCustomFieldObjectsByName("Escalate to").first()


def optionsManager = ComponentAccessor.getOptionsManager()
def config = escalate.getRelevantConfig(issue)
def options = optionsManager.getOptions(config)
def optionToSelect = options.find {
it.value == "System Support"
}
escalate.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(escalate), optionToSelect), new DefaultIssueChangeHolder())
Like # people like this
Dean Stearn December 31, 2018

Thank you @Tarun Sapra

That code has worked perfectly for me.

Kind regards

Dean

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.
December 31, 2018

Hello @Dean Stearn

Great to know that it worked, please accept/upvote the answer so that others are also helped. thanks.

Kelly Turdanes December 28, 2020

Hi There,

I've been trying this code sample in our environment.

I am trying the same thing - updating a Select custom field with a value.

When I tried to Test the code, I am not getting a Result nor Log. Is this expected? Also the issue key where I am trying to test the code to is not updated with the value I added.

Not sure what I am missing. Is this code applicable to JSD server version?

Thank you

Kelly

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 30, 2018

Most of what you need is at https://library.adaptavist.com/entity/update-the-value-of-a-custom-field-in-jira 

(We Adaptavists are, at last, trying to build libraries of common functions.  It's still quite sparse at the moment though, as we're still not putting a huge effort in - there's a big change coming to ScriptRunner that will change the needs of such a library quite a lot, so we're all kind of hanging back until it is deployed)

The missing bit is that to set a select-list type field, you need to find the option object you want to set it to, to be passed into the update field call.  I've usually used OptionsManager.findByOptionValue("string holding name of option") , but that is a bit weak if you might have more than one option with the same name.

Dean Stearn December 31, 2018

Thank you @Nic Brough -Adaptavist-, I will try this suggestion.

regards

Dean

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events