script runner post function custom script doesn't do anything

 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
Accepted answer

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).

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.

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 ...

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?

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 ?

 

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.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Jira

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,082 views 0 8
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