Forums

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

Jira ScriptRunner - Groovy: Compute the Sum of Two Custom Fields & Input into Another Field

Akash L November 16, 2022

Hi all,

I'm new to coding in Jira ScriptRunner in Groovy. I am trying to write a script as a post-function after the "Create" transition that will automatically run when a user creates an issue in this particular project.

The two fields "CField" and "NField" are single-select drop-down fields with text values as options that include a "Score". The user must select a single drop-down option. The options are Strings, such as "(Score 0)", "(Score 100)", etc.

I have to get the score values the user has chosen for the two custom fields, convert them to an integer representation, sum up the two score values chosen for fields CField and NField, and input them into a new field called "Sum of Scores".

However, when this is run, I cannot even create an issue. I get an error upon trying to create the issue saying:

Error.png

In the Create transition, I’m using the Custom script post-function [ScriptRunner]:

postfunction.png

Here is my script:

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.component.ComponentAccessor;

def issueManager = ComponentAccessor.getIssueManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def CField = customFieldManager.getCustomFieldObject("customfield_12600") // Custom field for 'C'

int CIntScore = 0

if (CValue != null) // Checking to see what option the user has chosen for the first custom field

{

    if(CValue == "(Score 0)") {

        CIntScore = 0 // Setting whatever option the user has chosen to an integer

    }

    if(CValue == "(Score 100)") {

        CIntScore = 100

    }

    if(CValue == "(Score 250)") {

        CIntScore = 250

    }

    if(CValue == "(Score 750)") {

        CIntScore = 750

    }

    if(CValue == "(Score 1500)") {

        CIntScore = 1500

    }

}

def NField = customFieldManager.getCustomFieldObject("customfield_12601") // Custom field for 'N'

int NIntScore = 0

if (NValue != null) // Checking to see what option the user has chosen for the second custom field

{

    if(NValue == "(Score 0)") {

        NIntScore = 0 // Setting whatever option the user has chosen to an integer

    }

    if(NValue == "(Score 100)") {

        NIntScore = 100

    }

    if(NValue == "(Score 200)") {

        NIntScore = 200

    }

    if(NValue == "(Score 300)") {

        NIntScore = 300

    }

}

int IntSumScores = CIntScore + NIntScore

def SumOfScoresField = customFieldManager.getCustomFieldObject("customfield_20500") // Custom field 'Sum of Scores' cf[20500]

issue.setCustomFieldValue(SumOfScoresField, IntSumScores)

1 answer

0 votes
Nic Brough -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.
November 16, 2022

Your script is not actually reading the contents of either of your fields.  You need to read the fields into NValue and Cvalue, not just create the two variables.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events