Forums

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

Need a groovy script to calculate 4 single select fields and update in 1 single select field

Jeevan Kumar October 13, 2024

Hello,

I need a script to calculate the 4 custom fields and based on that calculation the result has to be updated in 1 single select custom field.

Below are the 4 custom fields with values Yes or No.
1. exposed services: Yes or No
     Yes = 2, No = 0
2. Monitoring: Yes or No
     Yes = 1, No = 0
3. Internet: Yes or No 
     Yes = 3, No = 0
4. Exploit: Yes or No
     Yes = 4, No = 0

if the total calculation of 4 custom fields is as follows then based on the total count, as per the below calculations the values has to be updated in the 5th custom field named "Result"

 

  • so, if the total count of these 4 custom fields value = 0 then it is "Rare"

 

  • if total count = 1 or 2 or 3 then it is "Unlikey"
  • if, total count = 4 or 5 or 6 then it is "likey"
  • if, total count = 7 or 8 or 9 or 10 then it is "Very likey"

for example,

if total count of 4 custom fields (exposed =Yes, Monitoring = No, Internet = No, Exploit = no) so total count = 2,
then Result = Unlikely.

AS per the above i need a script for this, please help me out.

 

Thanks & Regards,

Jeevan

2 answers

0 votes
Radek Dostál
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.
October 14, 2024

Not a huge fan of the syntax here but on first look at seems correct I think. Is "Result" a text field? If it's not then that is a problem, but otherwise this should generally work to calculate the value.

So if Result is a text field, what is the specific error you're getting?

0 votes
Jeevan Kumar October 13, 2024

Hello,

 

Below is the script i have tried but scriptrunner is giving errors in the def part of the script, i'm unable to run the script without getting any errors. please check and help me out.

 

// Import Jira components

import com.atlassian.jira.component.ComponentAccessor

 

// Get custom field manager to retrieve custom fields

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

// Get the current issue object

def issue = ComponentAccessor.getIssueManager().getIssueObject(issue.key)

 

// Define the custom field names

def exploitableField = customFieldManager.getCustomFieldObjectByName("Is the system exploitable from exposed services (Internally/Externally)?")

def monitoringField = customFieldManager.getCustomFieldObjectByName("Is there no monitoring or alerting mechanism in place for this issue?")

def exposedField = customFieldManager.getCustomFieldObjectByName("Is the system exposed to the internet?")

def exploitField = customFieldManager.getCustomFieldObjectByName("Is there an active exploit for this issue?")

def resultField = customFieldManager.getCustomFieldObjectByName("Result") // Updated field name to "Result"

 

// Get the values of the custom fields for the current issue

def exploitableValue = issue.getCustomFieldValue(exploitableField)?.toString()

def monitoringValue = issue.getCustomFieldValue(monitoringField)?.toString()

def exposedValue = issue.getCustomFieldValue(exposedField)?.toString()

def exploitValue = issue.getCustomFieldValue(exploitField)?.toString()

 

// Initialize total score to 0

int totalScore = 0

 

// Logic to assign score based on custom field values

// If "Yes" = certain points, if "No" = 0 points

if (exploitableValue == "Yes") {

    totalScore += 2

}

if (monitoringValue == "Yes") {

    totalScore += 1

}

if (exposedValue == "Yes") {

    totalScore += 3

}

if (exploitValue == "Yes") {

    totalScore += 4

}

 

// Determine the Result value based on the total score

String result = ""

 

if (totalScore == 0) {

    result = "Rare"

} else if (totalScore == 1) {

    result = "Unlikely"

} else if (totalScore == 2) {

    result = "Unlikely"

} else if (totalScore == 3) {

    result = "Unlikely"

} else if (totalScore == 4) {

    result = "Likely"

} else if (totalScore == 5) {

    result = "Likely"

} else if (totalScore == 6) {

    result = "Likely"

} else if (totalScore == 7) {

    result = "Very Likely"

} else if (totalScore == 8) {

    result = "Very Likely"

} else if (totalScore == 9) {

    result = "Very Likely"

} else if (totalScore == 10) {

    result = "Very Likely"

}

 

// Set the value of the "Result" custom field

def issueService = ComponentAccessor.getIssueService()

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 

// Update the field with the calculated result value

def updateIssueInputParameters = issueService.newIssueInputParameters()

updateIssueInputParameters.addCustomFieldValue(resultField.id, result)

 

def validationResult = issueService.validateUpdate(user, issue.id, updateIssueInputParameters)

 

if (validationResult.isValid()) {

    issueService.update(user, validationResult)

} else {

    log.error("Failed to update the issue: ${validationResult.getErrorCollection()}")

}

 

Thanks & Regards,

Jeevan

Suggest an answer

Log in or Sign up to answer