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.
×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
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.