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"
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
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?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.