Scriptrunner post-function question

I am trying to set value of custom field "Billing" (type: single select) to "Billable" in create issue post function. Field "Billing" has 2 options: "Billable" and "Non-Billable". Value "Non-Billable" is set as fields default value.
For setting the field we are using scriptrunner scripted post function. code:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.customfields;

def cf2 = customFieldManager.getCustomFieldObjects(issue).find { == "Billing"}
def fieldConfig = cf2.getRelevantConfig(issue);
value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Billable' }
issue.setCustomFieldValue(cf2, value);

I can't make it work.
Can somebody help me to figure this out ?

Thank you in advance!

6 answers

1 accepted

This widget could not be displayed.

Where is customFieldManager defined? You need:

def customFieldManager = ComponentAccessor.getCustomFieldManager()

Defining customFieldManager helped - no errors in logs, but the field still has its default value. I tried to change the fields default value to "None" the field was empty after creating a new issue. Strange is that on other transitions the scripted post function works, only on create issue I am unable to get it work. It seems that problem is positioning of the post function in create issue. What do you think about it?

for the API you've used, the post-function needs to be at the top of the list of post-functions.

Thank you Jamie! It works now.

Hello Jamie, I would like to ask one additional question. It is possible on create issue transition setup a custom field value depending on the issue key. I mean something like this: {source} if (issue.key.contains("TBT")) { set field value } {source} Using this approach is not possible because if the post function is placed before "creates issue originally" it throws an null pointer exception.

This widget could not be displayed.

Looks reasonable... is that the first post-function? Any errors in logs?

This widget could not be displayed.

Hello Jamie, error was with the last import I fixed this by putting this lane as comment. trying running the script again ended in this error: "at groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script54"

This widget could not be displayed.

Scripted post-function is not first but is right after post function "Creates the issue originally."

This widget could not be displayed.

@Jamie Echlin [Adaptavist] anything new in this matter? Can you help with it?

This widget could not be displayed.

Hi Julius

If you are still looking for this, I found this worked as a way of updating a custom field:

DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder()
    customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), value.toString()), issueChangeHolder)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

533 views 3 19
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