I am looking for some assistance in a Groovy script in Scriptrunner that will add a value from the system "Priority" field to a value from a custom field (single choice) and populate the total into a 3rd field that is my end-point. I am able to do this with the custom field, but I am having major issues trying to pull a single numeric value from the "Priority" field. My end result should be very simple, but I have spent hours trying to make sense of the Priority field. All I need to do is; A + B = C...which in this case is;
A. "Client Health"[custom field] with 4 single pick choices
B. A value from the system "Priority" field, but based on a reverse numbering criteria
C. Resulting single digit "Churn Risk" field with A + B
Here is my script so far. I can get A working just fine (populates C with A+A for example). It is B that I cannot get to work. I did have it doing some form of calculation at one point, but there were 4 leading numbers in there (so 5 was showing as "10404").
import java.lang.Integer
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.priority.Priority
def customFieldManager = ComponentAccessor.customFieldManager
def clientHealth = customFieldManager.getCustomFieldObjectsByName('Client Health').first()
def clientHealthValue = issue.getCustomFieldValue(clientHealth)
def clientHealthValueMap = ['0 - Black':0, '1 - Green':1, '3 - Yellow':3, '5 - Red':5] as Map<String, Long>
def reporterPriority = issue.priority.id
def priorityLevelValueMap = ['P1 - Critical':4, 'P2 - High':3, 'P3 - Medium':2, 'P4 - Low':1] as Map<String, Long>
if (clientHealth) {
def option1 = clientHealthValueMap[clientHealthValue.toString()] as Long
def option2 = Integer.parseInt(issue.priority.id)
(option2 + option2)
}
else {
0
}
//Any help would be greatly appreciated!!!
Could you please clarify what ScriptRunner feature you are using to do this calculation? Is it the Scripted Field?
I am requesting this so I can provide an example.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For your requirement, you will need to modify your Scripted Field code to this:-
def sampleListValue = issue.getCustomFieldValue('Sample List')
def priority = issue.priority.name
def sampleListValueMap = ['Option 1':100, 'Option 2':75, 'Option 3':50, 'Option 4':25] as Map<String, Long>
def priorityTypeValueMap = ['Highest':100, 'High':75, 'Medium':50, 'Low':25, 'Lowest':10] as Map<String, Long>
if (sampleListValue && priority) {
def option1 = sampleListValueMap[sampleListValue.toString()] as Long
def option2 = priorityTypeValueMap[priority] as Long
(option1 + option2) as Long
} else {
0
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Scripted Field configuration:-
Below are a couple of test screenshots for your reference:-
As you can see in the screenshots above, when the option from the Single Select List and Priority fields are updated, the value of the Scripted Field is updated accordingly.
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ram Kumar Aravindakshan _Adaptavist_ thanks so much for your help. What I neglected to mention is that the custom scripted field will only be displayed in a linked ticket (a bug ticket for example), so I need the script to pull the "Priority" and "Client Health" values from the originating ticket into the linked ticket. How would I reference this in the script? Here is what I have working so far:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried the following, but was getting an error in Scriptrunner:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you are trying to get the total impact from linked issues, you need to follow the working code below:-
def outwardLinks = issue.getOutwardLinks { excludeSystemLinks = true }
def sampleListValueMap = ['Option 1':100, 'Option 2':75, 'Option 3':50, 'Option 4':25] as Map<String, Long>
def priorityTypeValueMap = ['Highest':100, 'High':75, 'Medium':50, 'Low':25, 'Lowest':10] as Map<String, Long>
def result
outwardLinks.each {
def linkedIssue = it.destinationObject
def sampleListValue = linkedIssue.getCustomFieldValue('Sample List')
def priority = linkedIssue.priority.name
if (sampleListValue && priority) {
def option1 = sampleListValueMap[sampleListValue.toString()] as Long
def option2 = priorityTypeValueMap[priority] as Long
result = (option1 + option2) as Long
} else {
result = null
}
}
result
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to update it accordingly.
Below is a screenshot of the updated Scripted Field configuration:-
Please also ensure that you update your ScriptRunner plugin to the latest release, i.e. 8.9.0 or at least 7.11.0, so you can make use of ScriptRunner's HAPI feature to simplify your code.
Below are a couple of test screenshots for your reference:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ram Kumar Aravindakshan _Adaptavist_ thanks again for sticking with me on this one. I will give this a go!
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.