Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

script runner post function custom script doesn't do anything

yannis meriel November 18, 2016

 Hi

i have a transition with a customfield ( type :  Hideable Free Text Field )
and the system field "Comment", set by default on my transition screen.

what i want to do is to copy the value of the comment into the customfield ( it will be hiden) on a postfonction custom script with script runner.

 

the customfield is also configured on the edit screen of the issue type i'm working on.

after searching, i came up with this code :

import com.atlassian.jira.issue.Issue 
import com.atlassian.jira.issue.MutableIssue 
import com.atlassian.jira.issue.ModifiedValue 
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder 
import com.atlassian.jira.component.ComponentAccessor

MutableIssue issue  = (MutableIssue) issue; 
def customFieldManager = ComponentAccessor.getCustomFieldManager() 
def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "test_Commentaire_client"} 
def changeHolder = new DefaultIssueChangeHolder() 
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField),transientVars.comment as string),changeHolder)

 

I do not have any errors on my logs , but the customfield is never updated after calling my transition.

Can you help me ?

3 answers

1 accepted

0 votes
Answer accepted
Jonny Carter
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.
November 18, 2016

You need to use issue.setCustomFieldValue to set the field's value in a postFunction. Also, use String, not 'string', which isn't a keyword in Java.

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
log.debug("Running script")
MutableIssue issue  = (MutableIssue) issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "test_Commentaire_client"}
def changeHolder = new DefaultIssueChangeHolder()
def comment = transientVars.comment as String
log.debug("$comment")
issue.setCustomFieldValue(tgtField, comment)
log.debug("Done")

 

As a side tip, you can put logging statements in your script to help you find its execution details in the logs. I have my logger set to show the debug statements shown above, but you'll probably have a higher setting by default (like log.warn).

0 votes
yannis meriel November 20, 2016

thx for your answer Jonny,

but it works only if i set my Hideable Free Text Field customfield to "visible".
If I hide it, there is no update and the field sad


I'll try to search why, but if you have any idea ...

Jonny Carter
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.
November 21, 2016

setCustomField should work, even on hidden fields. How are you checking the value of the field? That is, how do you read the value of the field once it's hidden to confirm its value isn't getting set. Maybe this is a new question in its own right?

yannis meriel November 21, 2016

I am checking the value in the database , table "customfieldvalue" .

When the field is visible , the value is updated, but when i hide it with the behavior plugin,

the value isn't getting updated.

So do you want me to create a new ticket for this ?

 

Jonny Carter
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.
November 28, 2016

Those hideable field types are a semi-experimental feature of ScriptRunner, and it occurs to me that what you're experiencing may be normal behaviour. Hideable fields marked as hidden by a behaviour can't even be read via the REST API. It would make sense to me that you wouldn't be able to set their value via the UI, as its existence ought to basically be hidden.

If you think that's not how it should work, feel free to raise an issue with us.

0 votes
Jonny Carter
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.
November 18, 2016

Last line might need to be changed to

tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField),transientVars.comment as String),changeHolder)

as 'string' isn't a valid keyword in java/groovy.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events