Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner post-function script for autoincrementing a numeric custom field

Becky February 9, 2024

We recently migrated from Jira server to cloud, which broke some of our custom post-functions.  That was completely expected, but I am having trouble recreating an autoincrement post-function that we had in our server instance.

The post-function increases the value of a numeric custom field by 1.  Below is the script we used in server.

Does anyone know how to translate that script into cloud-speak?  I have limited technical chops and have not been able to find a solution in the forums.  Thanks in advance for any ideas or guidance.

// 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
//
// String customFieldName = "Custom field name"
// 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)

 

3 answers

1 accepted

0 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Champion
February 12, 2024

Hi Becky,

I can confirm that I have an example script located here using ScriptRunner for Jira Cloud which shows how to increment a number field by a specified value each time an issue is transitioned.

This script should be run as a Run Script Post function as the Initiating User and can be used as a guide to help create the required script.

I hope this helps.

Regards,

Kristian

Becky February 12, 2024

That worked perfectly, thank you Kristian!

Kristian Walker _Adaptavist_
Community Champion
February 13, 2024

Hi Becky,

I am glad we were able to help solve your requirement.

Regards,

Kristian

0 votes
Nic Brough -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.
February 9, 2024

Hi Becky,

You might find https://library.adaptavist.com/entity/calculate-custom-field-on-issue-update?tab=cloud useful as an example.  It calculates a field based on a couple of others, so it looks at more fields than you need, and you should actually be able to simplify it!

Becky February 9, 2024

Thanks Nic!  From a quick pass I do understand how that script would be modified to increase a number value from 0 to 1, but I will see if I can make sense of it next week when my brain is more awake.

Nic Brough -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.
February 9, 2024

The script gets and sets custom fields, so converting it to "read one custom field content, add one, and write it back" should not be too hard.

But yes, it's Friday, we're probably all tired, and could very much benefit from a weekend off!  Give it a shot next week and if you get stuck, let us know here!

Like Becky likes this
0 votes
Trudy Claspill
Community Champion
February 9, 2024

Hello @Becky 

Welcome to the Atlassian community.

I personally don't have a lot of experience with ScriptRunner scripts so I don't readily have the solution available to share. 

For ScriptRunner there is documentation concerning rewriting scripts:

https://docs.adaptavist.com/sr4jc/latest/scriptrunner-migration/migrate-from-scriptrunner-for-jira-server-to-cloud/rewriting-scripts-for-cloud-hints-and-tips

Have you had a chance to review that material? Is there another engineer in your organization with "technical chops" that you could engage in the script rewriting efforts?

 

Do you have many scripts that need to be rewritten? If you do you may want to consider engaging an Atlassian Solution Partner to get help with that.

(Disclosure: I work for Praecipio, which is an Atlassian Platinum Solution Partner operating in North America. Many of my colleagues have worked on ScriptRunner script rewrites for Server to Cloud migrations.)

Becky February 9, 2024

Hi Trudy, Thanks very much for the welcome and link to the documentation on rewriting scripts.  I will review that documentation and see if I can figure out how to update the script myself.  There is no engineer who can help, and we unfortunately do not have budget for a consultant.  

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events