Trying to use 2 custom fields to determine a scripted field value using ScriptRunner Script Fields

Amanda Tippett April 13, 2023

Hi,

I have 2 custom fields configured as Select List (single choice) which is completed on a screen during the workflow's 1st transition.  These 2 fields are used to determine the 3rd custom field which is configured as a Scripted Field.

Below is the script however when using the PREVIEW option, it always returns a NULL result even though the Preview Issue Key used has values that are in the def values section.  Configure Context and Screens has been completed.

I have also created a ticket using both scenarios in the def values section however the scripted field is not displaying on the screen configured.

Would greatly appreciate any advice on how to rectify this issue.

 

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

@BaseScript FieldBehaviours fieldBehaviours

int consequenceId = 17301
int likelihoodId = 17300
int targetFieldId = 17302

def consequence = getFieldById("customfield_" + consequenceId)
def consequenceValue = consequence.getValue()
def likelihood = getFieldById("customfield_" + likelihoodId)
def likelihoodValue = likelihood.getValue()
def targetField = getFieldById("customfield_" + targetFieldId)

def values = [[Consequence:"Severe", Likelihood:"Certain", TargetValue:"High"],
[Consequence:"Minimum", Likelihood:"Rare", TargetValue:"Low"]]

def targetFieldValue = values.find { it.Consequence == consequenceValue &&
it.Likelihood == likelihoodValue}?.TargetValue
if (targetFieldValue != null){
targetField.setFormValue(targetFieldValue)
}

3 answers

2 accepted

1 vote
Answer accepted
Sachin Dhamale
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 13, 2023

@Amanda Tippett

 

If you are using scripted field then you can update them in post function. 

 

if you are using the behavior script then you can use normal text field to update.

 

Thanks,

Sachin

Amanda Tippett April 23, 2023

Thank you @Sachin Dhamale this assisted me in getting the results I required.

Although it is painfully that with the behavior script the value does not appear until after a transition has happened.  Found it is a known issue that has been around for some time.

0 votes
Answer accepted
Amanda Tippett April 23, 2023
This is the behavior script that now works with the targetField a custom text field and the consequence and likelihood fields list single choice fields.
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

int consequenceId = 17301
int likelihoodId = 17300
int targetFieldId = 17309

def consequence = getFieldById("customfield_" + consequenceId)
def consequenceValue = consequence.getValue()
def likelihood = getFieldById("customfield_" + likelihoodId)
def likelihoodValue = likelihood.getValue()
def targetField = getFieldById("customfield_" + targetFieldId)

def values = [[Consequence:"Severe", Likelihood:"Certain", TargetValue:"Reportable"],
[Consequence:"Severe", Likelihood:"Likely", TargetValue:"Reportable"],
[Consequence:"Severe", Likelihood:"Possible", TargetValue:"Reportable"],
[Consequence:"Severe", Likelihood:"Unlikely", TargetValue:"Reportable"],
[Consequence:"Severe", Likelihood:"Rare", TargetValue:"Reportable"],
[Consequence:"Major", Likelihood:"Certain", TargetValue:"Reportable"],
[Consequence:"Major", Likelihood:"Likely", TargetValue:"Reportable"],
[Consequence:"Major", Likelihood:"Possible", TargetValue:"Reportable"],
[Consequence:"Major", Likelihood:"Unlikely", TargetValue:"Reportable"],
[Consequence:"Major", Likelihood:"Rare", TargetValue:"Reportable"],
[Consequence:"Moderate", Likelihood:"Certain", TargetValue:"Investigate"],
[Consequence:"Moderate", Likelihood:"Likely", TargetValue:"Investigate"],
[Consequence:"Moderate", Likelihood:"Possible", TargetValue:"Investigate"],
[Consequence:"Moderate", Likelihood:"Unlikely", TargetValue:"Investigate"],
[Consequence:"Moderate", Likelihood:"Rare", TargetValue:"Review"],
[Consequence:"Minor", Likelihood:"Certain", TargetValue:"Review"],
[Consequence:"Minor", Likelihood:"Likely", TargetValue:"Review"],
[Consequence:"Minor", Likelihood:"Possible", TargetValue:"Review"],
[Consequence:"Minor", Likelihood:"Unlikely", TargetValue:"Track"],
[Consequence:"Minor", Likelihood:"Rare", TargetValue:"Track"],
[Consequence:"Minimum", Likelihood:"Certain", TargetValue:"Track"],
[Consequence:"Minimum", Likelihood:"Likely", TargetValue:"Track"],
[Consequence:"Minimum", Likelihood:"Possible", TargetValue:"Track"],
[Consequence:"Minimum", Likelihood:"Unlikely", TargetValue:"Track"],
[Consequence:"Minimum", Likelihood:"Rare", TargetValue:"Track"]]

def targetFieldValue = values.find

{ it.Consequence == consequenceValue && it.Likelihood == likelihoodValue}
?.TargetValue
if (targetFieldValue != null)
{ targetField.setFormValue(targetFieldValue) }
0 votes
Marjina Konile April 14, 2023

Instead of using getValue() to retrieve the field values, try using getSelectedValue() to get the selected option value from the select list. For example:

def consequenceValue = consequence.getSelectedValue() def likelihoodValue = likelihood.getSelectedValue()

Also, ensure that the custom field IDs consequenceId, likelihoodId, and targetFieldId are correct and correspond to the correct custom fields.If the script still doesn't work, you may want to try debugging it by adding some log.debug() statements i hope it will works for you because when i was facing this issue in my project hybrid matress.it was solved 

thnK regarda

Amanda Tippett April 23, 2023

Thank you for your advice @Marjina Konile , afraid the getSelectedValue() did not help in this case.

Suggest an answer

Log in or Sign up to answer