Hi I'd like to increment a custom field when certain transitions take place. The default value is 0 and should count upwards by 1 each time a post function is run. I've searched around and found a few different answers but when I put any of them into the Code box all the import statements show "unable to resolve X".
Does anyone have a scriptrunner script I can use? Thanks!
Hello,
Could you provide the script which is not working?
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue;
Long customFieldId = 10065;
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObjectByName(customFieldId);
Double currValue = (Double)cf.getValue(issue);
Double newValue = currValue+1;
cf.updateValue(null, issue, new ModifiedValue(currValue,newValue), changeHolder);
I found it here:
When I paste that into the code box all the import statements show an X and can't resolve Name.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Change the script to this one and replace customFieldName with the name of your custom field
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue;
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObjectByName("customFieldName");
Double currValue = (Double)cf.getValue(issue);
Double newValue = currValue+1;
cf.updateValue(null, issue, new ModifiedValue(currValue,newValue), changeHolder);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How do I define "customFieldName"? The page I linked says to use a Long but your response suggests a String. If the field has ID 10065 and is named "CR Ready" what should the parameter value be?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You define it "CR Ready"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello, I'm a beginner with the scripts and I want to do the same thing as Jonathan but for how many times an issue has been reopened. So I entered this script in the Reopen transition and I created a numeric custom field with a default value of 0.
Each time I test it with a new request, I get this error :
java.lang.NullPointerException: Cannot invoke method getValue() on null object
I'm pretty sure it's really easy to fix but everything is new to me so... :|
What should I do?
Thanks!
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.