How to create a post function script?

Good day.

 

We are currently using Scriptrunner 3.0.16 and plan to upgrade to 4.1.3.8 or 4.2.0.5 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 {it.name == "sumNumber"}
def tgtField1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "number1"}
def tgtField2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

113 views 0 3
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