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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

My code is not working properly in Script Runner for few of the switch cases.

Hi, 

I have developed a custom scripted field for calculating JDP Risk Score using Script Runner Tool. We have also written the code for the field but code is not working completely.

I am attaching the code here.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Logger

/**
* Script which reads text value from two custom fields and converts then to a numeric value.
* By default, two custom fields which are taken into consideration:
* - 'Risk probability'
* - 'Risk consequence'
*
* For each field a set of possible values is defined in this script.
* Each value is assigned a numerical value which is later used for multiplication.
* Assignment of text to a number is done in 'switch' blocks.
* To map a new text value to a number, insert 3 lines:
* case "Input the text value to map":
* consequenceNumberValue = "Input the number to be mapped as a n integer e.g. 5";
* break;
* Example of such new mapping for Risk consequence:
* case "Critical":
* consequenceNumberValue = 9;
* break;
* Example for new Risk probability mapping:
* case "Very low":
* probabilityNumberValue = 1;
* break;
*
* Script can be extended to calculate the Ranking with different formula and using different amount of data.
* */

def log = Logger.getLogger("scripts.risk.RiskRankingFieldFormula")
Issue issue = issue;
String riskProbabilityName = "Risk probability";
String riskConsequenceName = "Risk consequence";
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
try {
List<CustomField> listOfConsequenceCustomFields = customFieldManager.getCustomFieldObjectsByName(riskConsequenceName);
CustomField riskCustomField;
if (listOfConsequenceCustomFields.isEmpty()) {
throw new Exception("Could not find a custom field with name '" + riskConsequenceName + "'");
} else {
riskCustomField = listOfConsequenceCustomFields.get(0);
}

String consequenceTextValue = issue.getCustomFieldValue(riskCustomField);
Integer consequenceNumberValue;

switch (consequenceTextValue) {
/** Insert new mappings after this line*/
case "Almost none":
consequenceNumberValue = 1;
break;
case "Low":
consequenceNumberValue = 2;
break;
case "Medium":
consequenceNumberValue = 3;
break;
case "High":
consequenceNumberValue = 4;
break;
case "Very high":
consequenceNumberValue = 5;
break;
default: //Setting the value to zero will force the multiplication
consequenceNumberValue = 0;
}

List<CustomField> listOfProbabilityCustomFields = customFieldManager.getCustomFieldObjectsByName(riskProbabilityName);
CustomField probabilityCustomField;
if (listOfProbabilityCustomFields.isEmpty()) {
throw new Exception("Could not find a custom field with name '" + riskProbabilityName + "'");

} else {
probabilityCustomField = listOfProbabilityCustomFields.get(0)
}
String probabilityTextValue = issue.getCustomFieldValue(probabilityCustomField)
Integer probabilityNumberValue;
switch (probabilityTextValue) {
/** Insert new mappings after this line*/
case "Trivial":
probabilityNumberValue = 1;
break;
case "Low":
probabilityNumberValue = 2;
break;
case "Medium":
probabilityNumberValue = 3;
break;
case "High":
probabilityNumberValue = 4;
break;
case "Severe":
probabilityNumberValue = 5;
break;
default:
probabilityNumberValue = 0;
}
/** Multiply both numbers values to receive a final Risk ranking as an integer*/
Integer calculatedRiskRanking = probabilityNumberValue * consequenceNumberValue;
return calculatedRiskRanking;

} catch (Exception e) {
log.error("Caught exception: " + e);
// e.printStackTrace()
/** Return -1 to indicate that something did not go as planning in the algorithm*/
return -1;
}

 

In this code, the Risk Score is not getting calculated for switch case of 'Almost none' and 'Very high' in Risk Consequence and for switch case of "Trivial' and 'Severe' in Risk Probability.

For switch cases of 'Low', 'Medium' and 'High', the code is working fine.

I am attaching the snapshot of error. Please guide us accordingly.

risk score 1.JPGrisk score 2.JPGrisk score 3.JPGrisk score 4.JPG

1 answer

0 votes

Could the two broken option names contain extra spaces or the space be a non-displayable character?  Those would mean the named strings are different to what you've got in your code.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

560 views 15 18
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you