Heads up! On March 5, starting at 4:30 PM Central Time, our community will be undergoing scheduled maintenance for a few hours. During this time, you will find the site temporarily inaccessible. Thanks for your patience. Read more.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Calculate Custom Fields values in Scripted Field using Scriptrunner in JIRA Cloud

Digvijay Singh Gehlot
Contributor
November 29, 2024

Hi Community, 

In Jira Cloud, I have two custom fields: Compliance and Audit (Select List - single choice).

I have created two Text Fields (single line): Risk Score and Risk Level which will be used as Scripted Field in Scriptrunner in Jira Cloud.

First, I am writing this below code to calculate the sum of Compliance and Audit and show the result in Risk Score:

def complianceField = "customfield_XXXXX" //Compliance
def auditField = "customfield_YYYYY" //Audit
def result = get("/rest/api/2/issue/${issue.key}")
             .queryString("fields", "customfield_XXXXX,customfield_YYYYY")
             .header('Content-Type', 'application/json')
             .asObject(Map)

String complianceFieldValue = result.body.fields[complianceField]?.value
String auditFieldValue = result.body.fields[auditField]?.value

def retval = 0
switch (complianceFieldValue) {
              case ('Impacts from 10 and above'):
                             retval+=9;
                             break;
              case ('Impacts from 5 to 10'):
                             retval+=5;
                             break;
              case ('Impacts from 3 to 5'):
                             retval+=3;
                             break;
              default:
                             retval+=0;
                             break;
}
switch (auditFieldValue) {
              case ('Impacts more than 1000 consumers'):
                             retval+=9;
                             break;
              case ('Impacts between 500 consumers'):
                             retval+=5;
                             break;
              case ('Impacts less than 300 consumers'):
                             retval+=3;
                             break;
              default:
                             retval+=0;
                             break;
}
return retval.toString();

But facing Static type checking error in the above code. Please guide further in resolving the issue.

3 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Bobby Bailey
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.
December 4, 2024

Hi @Digvijay Singh Gehlot , 

The issue you have here is that with the .asObject(Map) call it doesn't map all levels. So, the error occurs because Groovy doesn't have the type information for result.body.fields[complianceField]?.value, and so it can't determine that there is a value element, as it defaults to casting it as an Object.

Just so you are aware, your code will still function, because it is there. This error only tells you that the editor cannot get the correct information, so is throwing what it thinks is an error. 

But, if you want to clear the error, we need to apply the map to the next levels. So:

def result = get("/rest/api/2/issue/${issue.key}")
             .queryString("fields", "customfield_XXXXX,customfield_YYYYY")
             .header('Content-Type', 'application/json')
             .asObject(Map)

String complianceFieldValue = result.body.fields[complianceField]?.value
String auditFieldValue = result.body.fields[auditField]?.value


Becomes: 

def result = get("/rest/api/2/issue/${issue.key}")
             .queryString("fields", "customfield_XXXXX,customfield_YYYYY")
             .header('Content-Type', 'application/json')
             .asObject(Map)
             .body

def issueFields = result.fields as Map

Map complianceFieldMap = issueFields[complianceField]
Map auditFieldMap = issueFields[auditField]

String complianceFieldValue = complianceFieldMap?.value
String auditFieldValue = auditFieldMap?.value


And


def result = get("/rest/api/2/issue/${issue.key}")
   .queryString("fields", "customfield_AAAAA")
   .header('Content-Type', 'application/json')
   .asObject(Map)



def riskScoreValue = result.body.fields["customfield_AAAAA"]
int riskScoreIntValue = riskScoreValue.toInteger()


Becomes: 

 

def result = get("/rest/api/2/issue/${issue.key}")
.queryString("fields", "customfield_AAAAA")
.header('Content-Type', 'application/json')
.asObject(Map)
.body

def issueFields = result.fields as Map
def riskScoreField = issueFields["customfield_AAAAA"] as Map

String riskScoreValue = riskScoreField.value
int riskScoreIntValue = riskScoreValue.toInteger()

 

If you could try those and let me know how you get on?

Kind regards, 

Bobby

0 votes
Daniel Alonso
Contributor
November 29, 2024

Hi @Digvijay Singh Gehlot 

I tried your code and it worked on my side. You just need to select the "Field Type" = "Short Text".

Where exactly you're seeing this error?

0 votes
Digvijay Singh Gehlot
Contributor
November 29, 2024

And in continuation to the above description, I am using Risk Level (Scripted Field) where I am checking if Risk Score is Low, Medium, High, or Very High as below:

def riskScore = "customfield_AAAAA"
def retval='Low'
def result = get("/rest/api/2/issue/${issue.key}")
.queryString("fields", "customfield_AAAAA")
.header('Content-Type', 'application/json')
.asObject(Map)
def riskScoreValue = result.body.fields["customfield_AAAAA"]
int riskScoreIntValue = riskScoreValue.toInteger()
if (riskScoreIntValue <= 10) {
              retval='Low';
    }
if (riskScoreIntValue > 30 && riskScoreIntValue < 50){
              retval='Medium';
    }
if (riskScoreIntValue > 50){
              retval='High';
}
return retval.toString();

And Static type checking is occuring in the above code.

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events