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

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
log.setLevel(Level.DEBUG)

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

//... excluded some markup to evaluate user email address and map domain to client... log.info("clientName: "+clientName); log.info("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"); log.info(customField.getName() + " 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) https://docs.atlassian.com/jira/server/com/atlassian/jira/issue/customfields/manager/OptionsManager.html for (option in options) { if (option.getRelatedCustomField().getCustomField().getName() == customField.getName()) {
issue.setCustomFieldValue(customField, option); log.info(customField.getName() + " 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 : jdoe@tampabay.rr.com 
2017-06-16 15:28:00,153 INFO [customfield.GroovyCustomField]: mailDomain : tampabay.rr.com 
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

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

log.setLevel(Level.DEBUG)

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

def clientName = "Acme"
def sourceName = "My Source"

log.info("clientName: " + clientName)
log.info("sourceName: " + sourceName)

//Set custom text field
def cfClient = cfm.getCustomFieldObjectByName("Client")
issue.setCustomFieldValue(cfClient,clientName)

//Set custom single select field
def customField = cfm.getCustomFieldObjectByName("Source")

log.info(customField.getName() + " 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)

log.info(customField.getName() + " customField after: " + issue.getCustomFieldValue(customField))

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

Best,

Aidan

Thanks Aidan!

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

BeforeTransition - Create Issue.PNG

After

after_Transition - Create Issue.PNG

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

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.

ftw.PNG

Happy to help!

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

Best of luck,                                                                                     Aidan

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Published Apr 19, 2018 in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

366 views 0 6
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you