I'm trying to set custom field values using ScriptRunner Transition: Create Issue Post function.
I have two custom fields:
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?
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:
In 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.
Before
After
ScriptRunner is now first. I'll report back with result.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It works! I did take your improved source with two modifications:
Now new raised requests come in with customfield values for both types. Thanks Again Aidan.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Happy to help!
We watch ScriptRunner tags pretty closely, so if you have any other questions, we'll be around :)
Best of luck, Aidan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great, you saved my day! I was pulling my hair out!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Axel Joester Good to know that past Me is still helping people out haha. :D
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.