cancel
Showing results for
Did you mean:
See all
See all
##### Groups
Explore all groups

# Determine custom field value based on 2 select lists

I have this requirement:

2 Select fileds:

Select field 1 (Impact) : Low Impact, Minor, Major, Unusable

Select field 2 (Probability) : Low, Medium, High, Crtical

Based on these selections another customer field (Severity) needs to be populated like Low/Low means Low, Unusable / Critical means High etc.

How can I achive this with script runner?

Appreciate your help (I am new to the scripting).

Thanks,

#### 1 accepted

There is a simpler ;-) example based on calculating a number based on some other issue fields here: https://jamieechlin.atlassian.net/wiki/display/GRV/Scripted+Fields#ScriptedFields-Calculateanumberbasedonotherfields

Hi Jamie,

I strated with calculating number example but it did not work. Here my example code

```def Impact = getCustomFieldValue("Customer Impact")
def Probability = getCustomFieldValue("Customer Probability")
def Severity = "None"

if(Impact &amp;&amp; Probability)
{
if( (Impact == "No Impact") &amp;&amp; (Probability == "Low") )
{
Severity = "Low"
}
}

return Severity```

Here Customer Impact & Customer Probability are select list and I selected return value as scripted field and "Free Text Field" for template.

Let me know what is wrong with it. It is always returning "None" here.

Also what is the easy way to debug these scripts (Any tools etc??).

Thanks,

There are several ways...

You could use a scripted field, read the values of the other fields and set the corresponding value. This is the easiest way.

If you want to use severity as a field within a statistic gadget, you have to write a listener which waits for changes to the other fields and sets the value of Severity accordingly. Severity has to be a select list (or an other type which is usable by statistc gadgets).

We use something like the following code to get something equal to your needs.

```import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.event.type.EventTypeManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.FieldManager
import org.apache.log4j.Logger

import static com.atlassian.jira.event.type.EventType.ISSUE_DELETED_ID
import static com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH

/**
* This class is used to update custom field Anomaly with the corresponding
* value with regard to Severity and Customer Value
*/

class AnomalyClassWriter extends AbstractIssueEventListener {

Logger log = Logger.getLogger(AnomalyClassWriter.class)
IssueManager issueManager = ComponentAccessor.issueManager
EventTypeManager eventTypeManager = ComponentAccessor.eventTypeManager
FieldManager fieldManager = ComponentAccessor.getFieldManager()
CustomFieldManager cfManager = ComponentAccessor.customFieldManager
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()

// With customEvent() and workflowEvent() we get all events
@Override
void customEvent(IssueEvent event) {
// only one central way...
this.workflowEvent(event)
}

@Override
void workflowEvent(IssueEvent event) {
MutableIssue mIssue
Map&lt;Long,EventType&gt; eventTypes = eventTypeManager.getEventTypesMap()

if (event.eventTypeId != ISSUE_DELETED_ID &amp;&amp; ['Bug', 'Request'].contains(event.issue.issueTypeObject.name)) {
if (event.issue instanceof MutableIssue) {
mIssue = (MutableIssue) event.issue
} else {
mIssue = issueManager.getIssueObject(event.issue.id)
}
updateAnomalyClassValue(mIssue, log)
} else {
}
}

void updateAnomalyClassValue (MutableIssue issue, Logger log) {

def cfSeverity = cfManager.getCustomFieldObjects(issue).find {it.name == 'Severity'}
def cfCustomerValue = cfManager.getCustomFieldObjects(issue).find {it.name == 'Customer Value'}
def cfAnomalyClass = cfManager.getCustomFieldObjects(issue).find {it.name == 'Anomaly Class'}

String anomaly

String severity = cfSeverity.getValue(issue)?.getValue()
String customerValue = cfCustomerValue.getValue(issue)?.getValue()

switch ([severity, customerValue]) {
case {it == [ null, null]}:         anomaly = null; break;
case {it == [ 'Low', null]}:
case {it == [ 'Other', null]}:
case {it == [ null, 'Low']}:
case {it == [ 'Other', 'Low']}:
case {it == [ 'Low','Low']}:        anomaly = '1'; break;
case {it == [ 'Low', 'Medium']}:
case {it == [ 'Other', 'Medium']}:
case {it == [ null, 'Medium']}:
case {it == [ 'Medium', 'Low']}:
case {it == [ 'Medium', null]}:
case {it == [ 'Medium', 'Medium']}: anomaly = '2'; break;
default:                            anomaly = '3'
}

def fieldConfig = cfAnomalyClass.getRelevantConfig(issue)
def newOption = optionsManager.getOptions(fieldConfig)?.find{it.value == (anomaly)}
issue.setCustomFieldValue(cfAnomalyClass, newOption)
issueManager.updateIssue(ComponentAccessor.jiraAuthenticationContext.getLoggedInUser(), issue, DO_NOT_DISPATCH, false)
}
}```

This listener uses the value of the field Severity and Customer Value to calculate Anomaly for issue types "Bugs" and "Request". Maybe this is a good starting point for your script.

Henning

> If you want to use severity as a field within a statistic gadget

I think you can get round this problem by using Jozef's natural searchers: https://marketplace.atlassian.com/plugins/sk.eea.jira.natural-searchers

Once I convert def to String it worked.

Thanks Henning for your nice Switch statement, which I used.

Can I ask what statements needed to be converted to string.

I converted the 3 field definitions to string so the values could be read out as string but when I run the script the resulting output is "class AnomalyClassWriter "

Community showcase
Published Mar 13, 2019 in Marketplace Apps

### Marketplace Spotlight: Marketing apps for Confluence to keep your teams working on the same page

&nbsp...

263 views 0 6

### Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Connect with like-minded Atlassian users at free events near you!

##### Find my local user group

Unfortunately there are no AUG chapters near you at the moment.