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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Groovy script to update a custom field

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

2 votes
Answer accepted
Tarun Sapra Community Leader Dec 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

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

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

Thank you @Tarun Sapra

That code has worked perfectly for me.

Kind regards

Dean

Tarun Sapra Community Leader Dec 31, 2018

Hello @Dean Stearn

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

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

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.

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

regards

Dean

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

165 views 0 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you