How to create a post function script?

Jeff Peters February 2, 2016

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

2 votes
Thanos Batagiannis _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 2, 2016

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
TAGS
AUG Leaders

Atlassian Community Events