Determine custom field value based on 2 select lists

Srikanth Vemuri May 1, 2013

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,

4 answers

1 accepted

2 votes
Answer accepted
JamieA
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.
May 2, 2013

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

Srikanth Vemuri May 2, 2013

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 && Probability)
{
    if( (Impact == "No Impact") && (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,

2 votes
Henning Tietgens
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.
May 1, 2013

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<Long,EventType> eventTypes = eventTypeManager.getEventTypesMap()

        if (event.eventTypeId != ISSUE_DELETED_ID && ['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

JamieA
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.
May 2, 2013

> 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

0 votes
Michael Clarke September 24, 2015

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 "

 

 

0 votes
Srikanth Vemuri May 2, 2013

Once I convert def to String it worked.

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events