Scriptrunner post-function question

Július Presinszky March 8, 2015

Hello,
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 {it.name == "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

2 votes
Answer accepted
JamieA
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.
March 10, 2015

Where is customFieldManager defined? You need:

def customFieldManager = ComponentAccessor.getCustomFieldManager()
Július Presinszky March 10, 2015

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?

JamieA
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.
March 10, 2015

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

Július Presinszky March 10, 2015

Thank you Jamie! It works now.

Samuel Titka March 11, 2015

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.

0 votes
Richard Lucas May 21, 2015

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)
....
0 votes
Július Presinszky March 10, 2015

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

0 votes
Július Presinszky March 8, 2015

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

0 votes
Július Presinszky March 8, 2015

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 Script54.run(Script54.groovy:6) groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script54"

0 votes
JamieA
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.
March 8, 2015

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events