Forums

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

Update a Custom Field when other Custom Fields are changed

David Harkins
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.
August 18, 2021

This piece is with regards to a Severity Matrix for our customer raised issues.

The following works fine via a script runner post function

import com.atlassian.jira.component.ComponentAccessor

def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Severity")
def cfConfig = cfSelect.getRelevantConfig(issue)

def ImpactField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10222")
def ImpactValue = (String)issue.getCustomFieldValue(ImpactField);

log.info("Priority:" + issue.priority.name )
log.info("Impact:" + ImpactValue )

def SeverityID = '0'

def IssueSeverity = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == SeverityID
}

if (ImpactValue == "Company (All)") {
if (issue.priority.name == "Critical") { SeverityID = '1'}
if (issue.priority.name == "Major") { SeverityID = '2'}
if (issue.priority.name == "Minor") { SeverityID = '3'}
}

if (ImpactValue == "Department (more than 1)") {
if (issue.priority.name == "Critical") { SeverityID = '2'}
if (issue.priority.name == "Major") { SeverityID = '2'}
if (issue.priority.name == "Minor") { SeverityID = '3'}
}

if (ImpactValue == "Individual") {
if (issue.priority.name == "Critical") { SeverityID = '3'}
if (issue.priority.name == "Major") { SeverityID = '3'}
if (issue.priority.name == "Minor") { SeverityID = '3'}
}

IssueSeverity = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == SeverityID
}
log.info("Severity Set To:" + SeverityID )

issue.setCustomFieldValue(cfSelect, IssueSeverity)

 

This works a treat on issue creation.  This is saved as a file in the script editor within Scriptrunner.

Now we need this to run whenever the Priority or Impact fileds are changed, my first attempt with a ScriptRunner Listner does not seem to work, though it appears to running the file.

Screenshot 2021-08-18 203407.pngScreenshot 2021-08-18 203331.png

1 answer

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
August 18, 2021

Hi @David Harkins Issue Events work with IssueEvent object. It is described in script runner docs: https://scriptrunner.adaptavist.com/5.6.8/jira/listeners.html#_custom_listeners

So at least you will need to get instance of an issue with:

def issue = event.issue
David Harkins
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.
August 18, 2021

@Martin Bayer _MoroSystems_ s_r_o__  

Is that then a modification the existing working code or do i need to duplicate it with some differences?

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
August 18, 2021

You can't simply reuse whole postfunction because listener works with different "core" object.

  • postfunction uses issue
  • listener uses issueEvent

But you do not need to duplicate all the code. You can create groovy class and use it. This class can contain almost all the code. DO you have some experiences with groovy/java or you need more help?

David Harkins
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.
August 18, 2021

Only experience I have is the pieces I have picked up 

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
August 18, 2021

Ok, try to duplicate the code first. It should be something like:

import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Severity")
def cfConfig = cfSelect.getRelevantConfig(issue)

def ImpactField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10222")
def ImpactValue = (String)issue.getCustomFieldValue(ImpactField);

log.info("Priority:" + issue.priority.name )
log.info("Impact:" + ImpactValue )

def SeverityID = '0'

def IssueSeverity = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == SeverityID
}

if (ImpactValue == "Company (All)") {
if (issue.priority.name == "Critical") { SeverityID = '1'}
if (issue.priority.name == "Major") { SeverityID = '2'}
if (issue.priority.name == "Minor") { SeverityID = '3'}
}

if (ImpactValue == "Department (more than 1)") {
if (issue.priority.name == "Critical") { SeverityID = '2'}
if (issue.priority.name == "Major") { SeverityID = '2'}
if (issue.priority.name == "Minor") { SeverityID = '3'}
}

if (ImpactValue == "Individual") {
if (issue.priority.name == "Critical") { SeverityID = '3'}
if (issue.priority.name == "Major") { SeverityID = '3'}
if (issue.priority.name == "Minor") { SeverityID = '3'}
}

IssueSeverity = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == SeverityID
}
log.info("Severity Set To:" + SeverityID )

issue.setCustomFieldValue(cfSelect, IssueSeverity)

give it a try and we can try to work on it... but unfortunetally I'm not able to teach you basics of object-oriented programming :( 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events