Forums

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

Groovy script to populate field with calculated values from custom & system fields

Paul Seekins August 17, 2023

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!!!


  

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 18, 2023

Hi @Paul Seekins

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

Paul Seekins August 18, 2023

@Ram Kumar Aravindakshan _Adaptavist_ thank you and yes, the destination field is a scripted field.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 21, 2023

Hi @Paul Seekins

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:-

scripted_field_config.png

Below are a couple of test screenshots for your reference:-

test1.png

test2.png

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

Paul Seekins August 21, 2023

@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:

def clientHealthListValue = issue.getCustomFieldValue('Client Health')
def priority = issue.priority.name

def clientHealthListValueMap = ['5 - Red':5, '3 - Yellow':3, '1 - Green':1, '0 - Black':0] as Map<String, Long>
def priorityTypeValueMap = ['P1 - Critical':4, 'P2 - High':3, 'P3 - Medium':2, 'P4 - Low':1] as Map<String, Long>

if (clientHealthListValue && priority) {
 def option1 = clientHealthListValueMap[clientHealthListValue.toString()] as Long
 def option2 = priorityTypeValueMap[priority] as Long
 (option1 + option2) as Long
} else {
 0
}




Paul Seekins August 21, 2023

I tried the following, but was getting an error in Scriptrunner:

def clientHealthListValue = issue.issuelinks.inwardIssue.getCustomFieldValue('Client Health')
def priority = issue.issuelinks.inwardIssue.priority.name
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 22, 2023

Hi @Paul Seekins

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:-

scripted_field_updated_config.png

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:-

image1.png

image2.png

image3.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Paul Seekins August 22, 2023

@Ram Kumar Aravindakshan _Adaptavist_ thanks again for sticking with me on this one. I will give this a go!

Suggest an answer

Log in or Sign up to answer