How to create a post function script?

Good day.


We are currently using Scriptrunner 3.0.16 and plan to upgrade to or in the coming months.

I am a non-developer/script guru looking for some guidance on creating several post function scripts to perform simple mathematical calculations with several numeric custom fields to populate another numeric field.  The following are the formulas:

(CF1 * CF2) + (CF3 * CF4) + CF5 = CF6

CF7 + CF8 + CF9 + ... + CFx = CFn

(CF20 / CF21)  * 100 = CF22


Many thanks for your assistance.

1 answer

Hi Jeff,

Here is a script example (as a custom script post function) that add the values of two custom fields, add put the sum as a value of a third custom field.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

Issue issue = issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField3 = customFieldManager.getCustomFieldObjects(issue).find { == "sumNumber"}
def tgtField1 = customFieldManager.getCustomFieldObjects(issue).find { == "number1"}
def tgtField2 = customFieldManager.getCustomFieldObjects(issue).find { == "number2"}

def number1 = issue.getCustomFieldValue(tgtField1)
def number2 = issue.getCustomFieldValue(tgtField2)

def changeHolder = new DefaultIssueChangeHolder()
tgtField3.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField3), number1 + number2),changeHolder)

I do not know your requirements but, if I may, you can also have a look at scripted fields. May fit better in your case.

Kind regards

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 13, 2019 in Marketplace Apps

Marketplace Spotlight: Marketing apps for Confluence to keep your teams working on the same page


275 views 0 7
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you