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

This widget could not be displayed.

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
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

509 views 3 18
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