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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

362 views 0 6
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