Calculate new field value based on other field values in Jira

Sneha Gopinath February 24, 2020

I need to calculate new field value based on other field value in current issue.  We need to write a custom script to achieve it.

For example, we have following fields  Estimate ( 1-5) ,Competitive (1-5), Customer Impact (1-3), Market Need (1-5) and Priority (1-4) 

When estimate is a , Competitive b, Customer Impact c, Market need d , priority  e, then new field value is x.

Please can someone help me with the custom script configuration.

Thank you in advance!

2 answers

0 votes
Laura Campbell _Seibert Media_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2020

Hi,

You could get the result you are looking for with calculated attributes in the app Elements Checklist.

In the example picture below, Status, Impact, and Probability are all select lists, but the list values could be numbers, letters, or anything else you need. The Risk column is a calculated attribute.

Checklist bug tracking carousel.png

In order to calculate the risk based on the choices made in the previous three columns, the app allows you to use logical expressions. Checklist risk matrix calculated attribute.png

 

This solution doesn't require any scripting, but it is a paid app and the Checklist would replace the custom fields you currently use.

Hope that helps!

Sneha Gopinath February 26, 2020

Hello Laura,

I installed the trial version plugin. But when I try to configure Structure, it is not accepting custom field value of type other than numbers.  Please can you let me know what am I missing.

screenshot.PNG

Thanks in advance!

Laura Campbell _Seibert Media_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 26, 2020

Hi, it looks like the syntax is not correct for the Estimate attribute. You'll need to put the name of the attribute in brackets and quotation marks like in the example I posted.

You can check out all the details about how advanced calculated fields can be configured here:

https://doc.elements-apps.com/cl/latest/elements-checklist-panels-configuration/structure/calculate-attributes-formulas

0 votes
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2020

Hi @Sneha Gopinath ,

Are you trying to automate this on a particular screen (i.e. dynamically change the value of the new field), or in a transition ?

Also, are those fields select lists?

Sneha Gopinath February 25, 2020

Hello Antoine,

Yes, those fields are of type select list dropdown.

Yes, trying to automate this on create screen.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2020

This is possible with scriptrunner. Do you have this app ?

Sneha Gopinath February 25, 2020

Yes we have scriptrunner plugin. Can you help us with this?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2020

Please add a behaviour on each of the fields but the target field. Use this script :

import groovy.transform.BaseScript

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

@BaseScript FieldBehaviours fieldBehaviours


int estimateId = 12501
int competitiveId = 12502
int customerImpactId = 12503
int marketNeedId = 12504
int targetFieldId = 12505

def estimate = getFieldById("customfield_" + estimateId)
def estimateValue = estimate.getValue()

def competitive = getFieldById("customfield_" + competitiveId)
def competitiveValue = competitiveId.getValue()

def customerImpact = getFieldById("customfield_" + customerImpactId)
def customerImpactValue = customerImpact.getValue()

def marketNeed = getFieldById("customfield_" + marketNeedId)
def marketNeedValue = marketNeed.getValue()

def priority = getFieldById(PRIORITY)
def priorityValue = priority.getValue().getName()

def targetField = getFieldById("customfield_" + targetFieldId)

def values = [[Estimate:"1", Competitive:"1", CustomerImpact:"1", MarketNeed:"1", priority: "Low", TargetValue: "1"],
[Estimate:"2", Competitive:"1", CustomerImpact:"1", MarketNeed:"1", priority: "Low", TargetValue: "2"],
[Estimate:"1", Competitive:"4", CustomerImpact:"1", MarketNeed:"1", priority: "Low", TargetValue: "5"]]

def targetFieldValue = values.find { it.Estimate == estimateValue && it.Competitive == competitiveValue && it.CustomerImpact == customerImpactValue && it.MarketNeed == marketNeedValue && it.priority == priorityValue}?.TargetValue
if (targetFieldValue != null){
targetField.setFormValue(targetFieldValue)
}

You need to adapt it with your field ids and your own data mapping (in the "values" grid). This will work if the fields are simple select list, you are using the native priority field and the target field is a text field. Let me know if you need some tweaking regarding the custom field types.

Sneha Gopinath February 26, 2020

Hello Antoine,

I have created all five custom fields as select list(single choice). For testing I added only 3 fields, please find the below script. I have added fields for screen.

import groovy.transform.BaseScript

//@BaseScript FieldBehaviours fieldBehaviours  (throws an error "Unable to Resolve Class")

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY


int estimateId = 12501
int competitiveId = 12502
int targetFieldId = 12505

def estimate = getFieldById("customfield_" + estimateId)
def estimateValue = estimate.getValue()

def competitive = getFieldById("customfield_" + competitiveId)
def competitiveValue = competitive.getValue()

def targetField = getFieldById("customfield_" + targetFieldId)

def values = [[Estimate:"1", Competitive:"1", TargetValue: "1"],
[Estimate:"2", Competitive:"2", TargetValue: "2"],
[Estimate:"3", Competitive:"1", TargetValue: "3"],
[Estimate:"2", Competitive:"2", TargetValue: "2"],
[Estimate:"3", Competitive:"3", TargetValue: "3"]]

def targetFieldValue = values.find { it.Estimate == estimateValue && it.Competitive == competitiveValue }?.TargetValue
if (targetFieldValue != null){
targetField.setFormValue(targetFieldValue)
}

But this is not working. Am I missing something?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 26, 2020

Hi, have you updated the custom field ids ? 

You can add 

import com.onresolve.jira.groovy.user.FieldBehaviours

so it does not throw an error. You might check the logs also to look for some error message. Next step would be to display the variable values in the script.

Suggest an answer

Log in or Sign up to answer