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,

4 answers

1 accepted

2 votes

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 && 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 Community Champion May 01, 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

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

 

 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

136 views 0 4
Read article

Atlassian User Groups

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

Find a group

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.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you