Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a script runner field

nhungttt148 April 2, 2024

Hi guys, 

I want to create a custom field called A that can be calculated: 

A = (customfield B - customfield C)/ count all number ticket of project X

Pls guide me in writing a runner script to create the above field

Thank you so much,

Note: Field B, C are datetime picker field type and A is number field

1 answer

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
April 2, 2024

Hi @nhungttt148 

There are some ambiguous parts (e.g. is custom field B and C a number field?) but you can use below code as a start

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def projectManager = ComponentAccessor.getProjectManager()

// Retrieve the custom fields by their names or IDs
// You can also use getCustomFieldObject("customfield_xxxx") to obtain by id
def fieldB = customFieldManager.getCustomFieldObjectByName("CustomField B")
def fieldC = customFieldManager.getCustomFieldObjectByName("CustomField C")

// Retrieve numeric values from custom fields B and C
def valueB = issue.getCustomFieldValue(fieldB) as Double // or you can use 'as Long', depending on the field's data type
def valueC = issue.getCustomFieldValue(fieldC) as Double

def project = projectManager.getProjectObjByKey("X")
def issueCount = issueManager.getIssueCountForProject(project.id)

def fieldValueA = (valueB - valueC) / issueCount

return fieldValueA

Suggest an answer

Log in or Sign up to answer