I have a Scripted custom field called FixVersion_Changed
The value of the field is updated as it captures value for number of changes made to the FixVersions field.
I would like to decrement the value of this field under certain conditions using a Scripted Post Function.
I am not a programmer and looking for a code that I can use to modify the value of this field.
you need to replace
ComponentAccessor.changeHistoryManager?.getChangeItemsForField(issue, "Fix Version").size() as int
with something like this
ComponentAccessor.changeHistoryManager?.getChangeItemsForField(issue, "Fix Version").findAll{ !it.getToString().isEmpty() }.size() as int
Unfortunately I can not check the code because I have no access to Jira right now. But the idea is that you have to count all changes of Fix Version field except when it was set to Null. I am not sure what getToString returns if value were set to null but maybe it will work.
Hello,
Use this code. It worked for me.
import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.changeHistoryManager?.getChangeItemsForField(issue, "Fix Version").findAll{ it.getToString() != null}.size()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
if you use scriptrunner scripted field then you will not be able to set a value for the field in a post-function. Scripted fields work differently. The value for the scripted field is calculated on showing the field on the screen. That is why you have to change the code for the field. I guess there is an increment somewhere in the code of the scripted filed. You need to add a condition which will skip the increment on your condition. If you produce the current code of the scripted filed I can give you more details.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Alex,
That makes sense..
The Scripted field name is FixVersion_Changed. Below is the code of the scripted field.
***
import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.changeHistoryManager?.getChangeItemsForField(issue, "Fix Version").size() as int
***
I would like to not increment the value of this field if I transition the workflow to Backlog and Deferred. This is because I clear the FixVersion field when I transition to Backlog or Deferred as a post-function. The issue could come back in the funnel at a later date and a new FixVersion could be assigned.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You need to add the post-function to a workflow step.
You could add a code something like this to decrement the value:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
// Replace 12345 by the ID of your customfield
def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(12345)
def fieldValue = issue.getCustomFieldValue(customField)
def newValue
// Add your conditions here
if (true){
newValue = fieldValue -1
}
def holder = new DefaultIssueChangeHolder()
customField.updateValue(null, issue, new ModifiedValue(fieldValue, newValue), holder)
You can modify the code, adjust it to your conditions to apply decrement.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Getting the below error...
Also if I specify the fieldID, I get few more errors.. Again I am not a Java or Groovy programmer to decipher
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Use spaces in your code. "fieldvalue-1" is a single object that you have not defined, not a calculation. Try "fieldvalue - 1"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you Nic and Tayyab. Alexey's solution below worked for me. I do have a use case for the code mentioned above as well. Will test it soon.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
What plugin do you use for the scripted field? Scriptrunner? If you are not sure then kindly produce the current code of the mentioned scripted filed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.