It's not the same without you
Join the community to find out what other Atlassian users are discussing, debating and creating.
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!
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.
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);
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?
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!
This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.
Read moreAtlas Camp is our developer event which will take place in Barcelona, Spain from the 6th -7th of September . This is a great opportunity to meet other developers and get n...
Connect with like-minded Atlassian users at free events near you!
Find a groupConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no AUG chapters near you at the moment.
Start an AUGYou're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.