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

Tony June 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
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

3 votes
Answer accepted
Aidan Derossett _Adaptavist_
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.
June 27, 2017

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

Tony June 27, 2017

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.

Tony June 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.

ftw.PNG

Aidan Derossett _Adaptavist_
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.
June 28, 2017

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 September 6, 2018

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

Aidan Derossett _Adaptavist_
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.
September 7, 2018

@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
TAGS
AUG Leaders

Atlassian Community Events