Forums

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

Validate 2 text custom fields and write a value in 3rd field with Script Runner

naim.yilmaz August 27, 2021

We are using Jira Server Software 8.5 and Adaptavist Script Runner Plugin.
How do i validate below condition with script runner? 

if a custom field choose= "A" and 2nd custom field choose ="B"
3rd custom field must write = "C"

or

if a custom field choose= "D" and 2nd custom field choose="E"
3rd custom field must write = "F"

or

if a custom field choose= "G" and 2nd custom field choose="H"
3rd custom field must write = "I"

Thanks Advance.....

2 answers

0 votes
naim.yilmaz August 30, 2021

Hi Peter,

Actually, if we create the 3rd field as the script field, wouldn't it work? If we write the script codes in the 3rd field (Risk Score field), can't we verify the other Probability and Influence fields and print the result? I attached pictures. :) Thanks advance for your support ...
riskscore.jpgriskscore2.jpg

PD Sheehan
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 30, 2021

Yes, of course, that can be accomplished with a scripted field.

But that was not the question.

naim.yilmaz August 30, 2021

My English is a little bad. I'm sorry. That's what I was trying to ask in the first. Can you help with my last question, please? What codes should I write in the Risk Score field?

naim.yilmaz August 31, 2021

Hi Again @PD Sheehan ,

I solved the problem as below. Is it the right approach? Do you think there is an error?

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def Field1 = customFieldManager.getCustomFieldObject("customfield_16776")
def Field2 = customFieldManager.getCustomFieldObject("customfield_14379")
def cFieldValue1 = issue.getCustomFieldValue(Field1)
def cFieldValue2 = issue.getCustomFieldValue(Field2)
def selectedValue1 = ((LazyLoadedOption)cFieldValue1).getValue()
def selectedValue2 = ((LazyLoadedOption)cFieldValue2).getValue()

if (selectedValue1 == "Lowest")
if (selectedValue2 == "Highest")
return "Medium";

if (selectedValue1 == "Lowest")
if (selectedValue2 == "High")
return "Medium";

if (selectedValue1 == "Lowest")
if (selectedValue2 == "Medium")
return "Low";

PD Sheehan
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 31, 2021

I"m assuming you'll need to account for all 5 possible values of probability and impact.

Maybe something like this?

https://cio-wiki.org/wiki/Risk_Matrix

Then if will be very ugly and hard to maintain to have a bunch of if groups.

This is much better:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def cfProbability = customFieldManager.getCustomFieldObject("customfield_16776")
def cfImpact = customFieldManager.getCustomFieldObject("customfield_14379")
def propbabilityOption = issue.getCustomFieldValue(cfProbability) as LazyLoadedOption
def impactOption = issue.getCustomFieldValue(cfImpact) as LazyLoadedOption
def probability = propbabilityOption.value
def impact = impactOption.value

def riskMap = [
Lowest: [Lowest:'Low', Low: 'Low', Medium: 'Low', High: 'Medium', Highest:'Medium'],
Low: [Lowest:'Low', Low: 'Low', Medium: 'Medium', High: 'Medium', Highest:'High'],
Medium: [Lowest:'Low', Low: 'Medium', Medium: 'Medium', High: 'High', Highest:'High'],
High: [Lowest:'Medium', Low: 'Medium', Medium: 'High', High: 'High', Highest:'High'],
Highest:[Lowest:'Medium', Low: 'High', Medium: 'High', High: 'High', Highest:'High'],
]

def risk = riskMap[probability][impact]
return risk

Just make adjustments to the values in quotes to decide what the return values should be for each combination.

Like naim.yilmaz likes this
naim.yilmaz September 1, 2021

Hello Peter,

It's work. Thank you very very very much. :) I wrote that you sent.

Thanks again

0 votes
PD Sheehan
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 27, 2021

I'll assume all 3 custom fields are "single select" type of fields.

One possible solution to automatically fill in a field like this is using scriptrunner behaviour.

Create a behaviour with the correct project/issue type mapping.

Add both field1 and field2 to the behaviour configuration and add the script below to BOTH field's server-side script (adjust field names and values):

import com.atlassian.jira.component.ComponentAccessor
def field1 = getFieldByName('Field 1')
def field2 = getFieldByName('Field 2')
def field3 = getFieldByName('Field 3')
def valueMap = [
A : [B : 'C', E:'Other'], //what if field 1 is A and field 2 is E? Then field 3 can be set to other
D : [E : 'F'],
G : [H : 'I'],
]

def value1 = field1.value as String
def value2 = field2.value as String

if(value1 && value2){
def value3 = valueMap[value1][value2]
if(value3){
//this line only works for scriptrunner 6.25 and up
field3.setFormValue(value3)
//use the following block for scriptrunner 6.24 or below
/*
def field3Cf = ComponentAccessor.customFieldManager.getCustomFieldObject(field3.fieldId)
def config = field3Cf.getRelevantConfig(issueContext)
def options = ComponentAccessor.optionsManager.getOptions(config)
field3.setFormValue(options.find { it.value == value3}.optionId)
*/
field3.setReadOnly(true) //lock the field if you don't want user to change the automatic selection
} else {
field3.setReadOnly(false) //allow the user to make a choice manually
field3.setHelpText("Choices in $field1 and $field2 don't map to an automatic value for $field3")
}
} else {
field3.setReadOnly(true) //locl the field if you want the user to try to set manually before making choices in other fields
field3.setHelpText('Make a choice in $field1.name and $field2.name first')
}

Suggest an answer

Log in or Sign up to answer