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

Scriptrunner post function: Update a custom text field with values from other custom fields

Tony Jun 16, 2017

JIRA v7.3.2 with Script Runner Add-On

I'm trying to set custom field values using ScriptRunner Transition: Create Issue Post function.

I have two custom fields:

  • Client = Text Field (single line)  
  • Source = Select List (single choice)  

I have the below inline script:


import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.ApplicationUser;
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def Issue issue = issue
def clientName = "Acme";
def sourceName = "My Source";

//... excluded some markup to evaluate user email address and map domain to client..."clientName: "+clientName);"sourceName: "+sourceName); //customfield_10104 = 'Client' Custom Field is freeformtext def cfClient = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10104"); ModifiedValue mVal = new ModifiedValue(issue.getCustomFieldValue(cfClient), clientName); cfClient.updateValue(null, issue, mVal, new DefaultIssueChangeHolder()); //customfield_10512 = 'Source' Custom Field is list
// option value does exist for: My Source def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10512"); + " customField before: " + issue.getCustomFieldValue(customField)); def fieldConfig = customField.getRelevantConfig(issue); def List options = ComponentAccessor.getOptionsManager().findByOptionValue(sourceName); //just get the options with same value rather than getOptions(fieldConfig) for (option in options) { if (option.getRelatedCustomField().getCustomField().getName() == customField.getName()) {
issue.setCustomFieldValue(customField, option); + " customField after: " + issue.getCustomFieldValue(customField)); //error: java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.jira.issue.customfields.option.Option //mVal = new ModifiedValue(issue.getCustomFieldValue(customField), sourceName); //customField.updateValue(null, issue, mVal, new DefaultIssueChangeHolder()); break; } }

When creating new issues within my project using this workflow, I don't see the values being set for custom fields. Can anyone point out if there are issues with this inline script that could be preventing update on these two custom fields?



If I take the same script above and add it to a seperate script runner custom field (<jiradomain>/plugins/servlet/scriptrunner/builtin?section=script_fields#) and preview result for same issue ID I see both custom fields types being set from log output:

2017-06-16 15:28:00,137 INFO [customfield.GroovyCustomField]: reporter user jdoe(jdoe) 
2017-06-16 15:28:00,153 INFO [customfield.GroovyCustomField]: email : 
2017-06-16 15:28:00,153 INFO [customfield.GroovyCustomField]: mailDomain : 
2017-06-16 15:28:00,153 INFO [customfield.GroovyCustomField]: clientName: Acme 
2017-06-16 15:28:00,153 INFO [customfield.GroovyCustomField]: sourceName: My Source 
2017-06-16 15:28:00,169 INFO [customfield.GroovyCustomField]: Source customField before: null 
2017-06-16 15:28:00,215 INFO [customfield.GroovyCustomField]: Source customField after: My Source

But am receiving error on left margin for line:


issue.setCustomFieldValue(customField, option);

[Static type checking] - cannot fine matching method com.atlassian.jira.issue.Issue#setCustomFieldValue(com.atlassian.jira.issue...., com.atlassian.jira.issue.customfields.option.Option). PLease check if the declared type is right and if the method exists.)




Any pointers to help me set custom field value for Select List (single choice)  custom field type on this version of Jira?


1 answer

1 accepted

2 votes
Answer accepted

Hey Tony,

When doing this on the Create issue transition, it's important to ensure that the post-function is put in its proper place amongst the execution history. In this case, the post function must be the FIRST thing ran, even before the issue is officially created. I'll attach a screenshot of what I'm talking about below:

Screen Shot 2017-06-27 at 11.31.29 AM.pngIn this example, the post-function is the first thing executed.

If you have this placement down, then it is usually a very easy process to set custom field values. I've made some changes to your code that I got working after a little bit of testing:


import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level


//Grab necessary Components
def cfm = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def clientName = "Acme"
def sourceName = "My Source""clientName: " + clientName)"sourceName: " + sourceName)

//Set custom text field
def cfClient = cfm.getCustomFieldObjectByName("Client")

//Set custom single select field
def customField = cfm.getCustomFieldObjectByName("Source") + " customField before: " + issue.getCustomFieldValue(customField))

def fieldConfig = customField.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig)
def option = options.find{it.value == "My Source"}

issue.setCustomFieldValue(customField, option) + " customField after: " + issue.getCustomFieldValue(customField))

 Try this out and see if it works for you :)



Tony Jun 27, 2017

Thanks Aidan!

I've updated the sequence of Transition - Create Issue Post Functions.

BeforeTransition - Create Issue.PNG


after_Transition - Create Issue.PNG

ScriptRunner is now first. I'll report back with result.

Tony Jun 27, 2017

It works! I did take your improved source with two modifications:

  • I repurposed customField variable for both custom field types.
  • Searching for corresponding option value using variable sourceName instead of literal value.

Now new raised requests come in with customfield values for both types. Thanks Again Aidan.


Happy to help!

We watch ScriptRunner tags pretty closely, so if you have any other questions, we'll be around :)

Best of luck,                                                                                     Aidan

Axel Joester Sep 06, 2018

Great, you saved my day! I was pulling my hair out! 

@Axel Joester Good to know that past Me is still helping people out haha. :D

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Marketplace Apps & Integrations

Demo Den Ep. 4: Continuous Integration with CircleCI & Jira Software

Get ready! Demo Den Episode 4 is coming your way on Tuesday, May 28, 2018 with a Continuous Integration and Delivery special demo. CircleCI Director of Solutions Engineering, Eddie Webb will show us ...

614 views 0 4
Join discussion

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