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

Update custom field with transition post-function

Dennis Poort December 7, 2015

Hi,

I want to copy a custom field (numeric) value to another custom field.

I'm using Script-Runner addon to call a custom inline script on post function for a transition that fires on every issue update (transitions: Any status -> Itself).

I can't seem to find the way to update a custom field. 

So for example:

"Custom field 1" = 7.5

"Custom field 2" = 3

When event is fired through workflow, Custom field 2 should also become 7.5. 

 

For all things I've tried, f.e. setCustomField, I run into methods not existing, etc.

 

JIRA version: 7.0.0

 ScriptRunner version: 4.2.0.2

 

Thanks.

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Maud Schlich January 16, 2016

Hi Dennis,

I have been told that the setCustomFieldValue is no longer working and you have to use something along the lines of Thanos answer in here: https://answers.atlassian.com/questions/33158582.

Hope this helps.

0 votes
Mario Günter
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.
December 7, 2015

Hi Dennis, 

please provide which versions you have. Both JIRA and Script-Runner. 

This would be an example.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
 
def cfManager = ComponentAccessor.getCustomFieldManager()
def cfCond = cfManager.getCustomFieldObject(11200L) // source field id
def value = cfCond.getValue(currentIssueObject).toString()

def cfITExec = cfManager.getCustomFieldObject(11310L) // target field id
currentIssueObject.setCustomFieldValue(cfITExec, value);

 

But also you could use a standard "copy value" workflow post-function in your workflow.

Hope this helps. 

Best Regards, 
Mario 

Dennis Poort December 7, 2015

Hi Mario, thanks for answering. I have updated my start post with versioning. With your script I am running into f.e. currentIssueObject being undefined. That said, with the ID's, is it the customfield ID followed by an "L"? Or does that ID come frome some place else? The copy function doesn't apply to Custom Fields does it?

Thanos Batagiannis _Adaptavist_
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.
December 7, 2015

Hi Dennis in the code that Mario posted you can use def targetField = customFieldManager.getCustomFieldObjectByName("CF name") def cfValue = issue.getCustomFieldValue(targetField)

Dennis Poort December 8, 2015

Thanks for replying. Didn't find that working for me. Still causing errors around methods not existing.

Mario Günter
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.
December 8, 2015

Dennis, could you please provide your error stack trace?

Dennis Poort December 11, 2015

Hi Mario, how exactly do I get that info for you?

Dennis Poort December 11, 2015

I now have the following code. I managed to get it working a bit earlier for a static value, but the script I have now does not work (but does not throw error in script input editor. Please note that the cfHoursToBill field is a scripted field which may be of importance: import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue def cfManager = ComponentAccessor.getCustomFieldManager() // Get value of already billed hours def cfAlreadyBilled = cfManager.getCustomFieldObjectByName("Uren gefactureerd") def cfAlreadyBilledValue = (double) cfAlreadyBilled.getValue(issue) // Get value of additional hours to be build def cfHoursToBill = cfManager.getCustomFieldObjectByName("Uren te factureren") def cfHoursToBillValue = (double) cfHoursToBill.getValue(issue) // Set value of already billed + to be billed as new value for already billed hours def newValue = cfHoursToBillValue + cfAlreadyBilledValue issue.setCustomFieldValue(cfAlreadyBilled, newValue);

TAGS
AUG Leaders

Atlassian Community Events