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 Risk Score & Level with Scripted Field using scriptrunner in JIRA Cloud

Digvijay Singh Gehlot
Contributor
November 28, 2024

Hi Community,

I want help on how to write the same scripted field groovy code using scriptrunner in JIRA Cloud as it is written in JIRA Data Center successfully.

I have two scripted fields in JIRA Data Center: Risk Score and Risk Level and I am able to configure them with the below code:

Risk Score:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;

def retval=0
def CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomField cf1 = customFieldManager.getCustomFieldObject('customfield_XXXXX'); // Compliance
def Object cfv1 = issue.getCustomFieldValue(cf1).toString()
switch (cfv1) {
              case ('Feature impacts more than 9+ customers'):
                             retval+=9;
                             break;
              case ('Feature impacts between 9 to 5+ customers'):
                             retval+=5;
                             break;
       case ('Feature impacts less than 5 to 3+ customers'):
                             retval+=3;
                             break;
              default:
                             retval+=0;
                             break;
}
def CustomField cf2 = customFieldManager.getCustomFieldObject('customfield_YYYYY'); // Audit
def Object cfv2 = issue.getCustomFieldValue(cf2).toString()
switch (cfv2) {
              case ('Feature requires L3 Process'):
                             retval+=9;
                             break;
              case ('Feature requires L2 Process'):
                             retval+=5;
                             break;
        case ('Feature requires L1 Process'):
                             retval+=3;
                             break;
              default:
                             retval+=0;
                             break;
}
return retval.toString();

The value obtained in Risk Score, will be compared with Risk Level as below:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;

def issueFactory = ComponentAccessor.getIssueFactory();
def issueObject = issueFactory.getIssue();
def retval='Low'
def CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomField cf1 = customFieldManager.getCustomFieldObject('customfield_AAAAA'); // Risk Score
def CustomField cf2 = customFieldManager.getCustomFieldObject('customfield_BBBBB'); // Risk Level

String cfv1 = issue.getCustomFieldValue(cf1).toString()
int cfv2 = Integer.parseInt(cfv1)
if (cfv2 < 5) {
              retval='Low';
    }
if (cfv2 > 5 && cfv2 < 10){
              retval='Medium';
    }
if (cfv2 > 10 && cfv2 < 20){
              retval='High';
}
if (cfv2 > 20) {
              retval='Very High';
}
return retval.toString();

I tried my best to write an appropriate groovy code but facing multiple issues like unable to import classes, syntax errors, and not able to use REST API in the code.

I am exploring the JIRA Cloud and it would be a great help, if you guide me with a sample code after reviewing my Jira data center code.

Thanks

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 4, 2024

 

Hi @Digvijay Singh Gehlot

If your objective is to perform the calculation on the Jira cloud, why are you using the Jira DC code format?

The ComponentAccessor is not usable on the Cloud. To invoke the field's value, you need to make REST requests.

Could you please share some screenshots of what you are trying to calculate? I require this so I can try to come up with a sample code.

Thank you and Kind regards,
Ram

0 votes
Digvijay Singh Gehlot
Contributor
November 28, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

May you help me with a sample code on how to calculate the sum of values of two custom fields select list (single choice): Compliance and Audit and the result should appear in Risk Score scripted field. 

Then compare Risk Score with another scripted field Risk Level to show High, Medium or Low. 

I have provided the codes of Risk Score and Risk Level present in Jira Data Center in my description. 

As I am exploring scriptrunner on Jira Cloud, it would be a great learning for me. 

Thanks

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events