Post function create: set custom field not setting on subtask using Groovy

The subtask creates based on the issue type selected....this works alone but when I add

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Department'}
issue.setCustomFieldValue(cf, "First Line")

in the Additional issue actions in the workflow editor


The subtask isn't created and obviously that custom field isnt updated.

Any ideas?

Thx in advance.

The result evaluated to *assertion error*
assert cfValues['Department'].class == "Open".class
       |       |              |     |         |
       |       Open           |     false     class java.lang.String
       |                      class com.atlassian.jira.issue.customfields.option.LazyLoadedOption

2 answers

1 accepted

Accepted Answer
3 votes

I managed to get it to work.

You can see what I added...

thanks again for the start

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager


def componentManager = ComponentManager.instance
def optionsManager = componentManager.getComponentInstanceOfType(OptionsManager.class)
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Department'}
if(cf!=null){
def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).find {it.value == "First Line"}
issue.setCustomFieldValue(cf, option)
}

nice one...

i think you are trying to set field value for select list, if yes then try with following code

def customFieldManager = componentManager.getCustomFieldManager()
def cf  = customFieldManager.getCustomFieldObjectByName("Department");
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)  
if(cf!=null){
IssueContextImpl issueContext = new IssueContextImpl(issue.getProjectObject(), issue.getIssueTypeObject());
def fieldConfig = cf.getRelevantConfig(issueContext)
def option = optionsManager.getOptions(fieldConfig).find {it.value == "First Line"}
issue.setCustomFieldValue(cf, option)
}

I tried this, but returned same result. No subtask created. Just the parent task.

It is a drop down list for this particular field yes.

can you share your code?

Hey again, thanks for the quick reply.

I didn't code much I just used Script Runner built in module in the work flow editor.

I copied your example as is into the additional options.

I'll upload screenshot.

Any further ideas? if it sa standard text box then this works perfect. but its a drop down.. so I dont know how to modify this to make the drop down be set specifically

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Department'}
issue.setCustomFieldValue(cf, 'my value')


Rambranam's code is more or less correct. Do you have a custom field option called "First Line" - case-sensitive?

There are a few examples on this forum on how to set a select list value.

Add some debug lines to make sure you are getting an option value.

Hey there..

I do have that as an option... its actually the first option....

javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: componentManager for class: Script75

This is the output from condition tester.. though I'm not sure if this can be tested there

Sorry Jamie,

I dont have much experience with this code or Jira in general. in reality I won't get much more complex here than populating fields automatically based off the issue type of the parent task.

I'm not sure how to add debug lines.. the syntax to get the information I'm looking for.

the parent task issuetype is "Support"

the subtask issuetype is "Technical Task"

Both screen layouts have the custom field drop down "Department"

I know the code looks sound as you said... I really dont understand why its not working....

looks like I might have to have people just do dupliacte work, add parent.. then manually add subtask with the fields manually being entered :(

Thanks for trying guys.

UPDATE: am getting this error repeatedly in the logs.


Script136.groovy: 5: unable to resolve class IssueContextImpl
@ line 5, column 18.
IssueContextImpl issueContext = new IssueContextImpl(issue.getProjectObject(), issue.getIssueTypeObject());
^

Script136.groovy: 5: unable to resolve class IssueContextImpl
@ line 5, column 33.
IssueContextImpl issueContext = new IssueContextImpl(issue.getProjectObject(), issue.getIssueTypeObject());

now am just getting this....

Caused by: groovy.lang.MissingPropertyException: No such property: componentManager for class: Script25

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

45 views 0 2
Join discussion

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