Scriptrunner behaviour: Using wildcards to find values

Dustin Glienke December 8, 2022

Hi there,

I'm trying to to set up a behaviour when a value contains a specific number:

 

def multiSelectField = getFieldById("customfield_164118");
def multiSelectFieldValue = multiSelectField.value as List
if (multiSelectFieldValue.contains(4000)) {
     wertcn.setFormValue("CN")
     wertus.setFormValue("US")
My value is Test_4000_test. Is there a way to use wildcards?

1 answer

1 accepted

1 vote
Answer accepted
Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2022

Hi @Dustin Glienke 

Can you try instead of contains() the following?

matches("4000")
Dustin Glienke December 8, 2022

Hi Alex,

thx for replying quickly. Unfortunately I'm receiving the following error:

2022-12-08 13_55_14-Window.png

Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2022

Is it possible to paste here you code so that I can perform some tests?

Dustin Glienke December 8, 2022

Yes of course:

import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

def material = getFieldById("customfield_164118");
def wertus = getFieldById("customfield_63900");
def wertcn = getFieldById("customfield_63901");

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger(getClass())

def multiSelectField = getFieldById("customfield_164118");

def multiSelectFieldValue = multiSelectField.value as List


// If value is null
if (multiSelectFieldValue == [null]) {
}

else if (multiSelectFieldValue.contains(4000)) {
     wertcn.setFormValue("CN")
     wertus.setFormValue("US")
}
the field material is a multi select list. Test value is Test_4000_test
Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2022

@Dustin Glienke just noticed on your script that on material you have this id:

customfield_164118

Is this correct? It has a six figure numerical id.

Dustin Glienke December 8, 2022

Yes it is correct:

2022-12-08 14_26_43-Window.png

Our system has a strange count system, we have not that many fields

Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2022

@Dustin Glienke the following script worked for me:

def text = getFieldByName("Text field (akox)")
def multiSelectField = getFieldById(getFieldChanged())
def multiSelectFieldValue = multiSelectField.value as List
def myValues =multiSelectFieldValue.toString()
def result = (myValues =~ /\d+/).findAll()

if (multiSelectFieldValue == [null] ) {
}
  else {
    assert result == ["4000"]
    text.setFormValue ("Found 4000")
  }
  • The above script was placed on my multi select field. So this is were you place it.
  • You replace the text variable with your wertus and wertcn variables
  • You place your setFormValues below the "assert"
  • You delete my text variable

 

Let me know if that works out for you!

Dustin Glienke December 8, 2022

Unfortunately it does not work but i don't know why:

def wertus = getFieldById("customfield_63900");
def wertcn = getFieldById("customfield_63901");

def multiSelectField = getFieldById(getFieldChanged())
def multiSelectFieldValue = multiSelectField.value as List
def myValues =multiSelectFieldValue.toString()
def result = (myValues =~ /\d+/).findAll()

if (multiSelectFieldValue == [null] ) {
}
  else {
    assert result == ["4000"]
wertcn.setFormValue("CN")
wertus.setFormValue("US")
//text.setFormValue ("Found 4000")
}
Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 8, 2022

Does anything happen or not?

Dustin Glienke December 8, 2022

Nothing happend

Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 10, 2022

@Dustin Glienke did you try following the guidelines I gave you with code? This code works on my instance without any problem. What kind of fields are wertcn and wertus?

Dustin Glienke December 13, 2022

Hi Alex,

I found a solution. The function contains is the right one. I figured out to use this 'value' instead of "value". Now it is working. Thanks for your help!

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

import org.apache.log4j.Logger

import org.apache.log4j.Level

import groovy.transform.BaseScript

def material = getFieldById("customfield_164118");

def wertus = getFieldById("customfield_63900");

def wertcn = getFieldById("customfield_63901");

def wertde = getFieldById("customfield_63902");

@BaseScript FieldBehaviours fieldBehaviours

def log = Logger.getLogger(getClass())

def multiSelectField = getFieldById("customfield_164118");

def multiSelectFieldValue = multiSelectField.value as List

def myValues =multiSelectFieldValue.toString()

// If value is null

if (myValues.contains('4000')) {

wertus.setFormValue("US")

}

if (myValues.contains('5000')){

     wertcn.setFormValue("CN")

}

if (myValues.contains('4000')==false) {

wertus.setFormValue("")

}

if (myValues.contains('5000')==false){

     wertcn.setFormValue("")

}

Suggest an answer

Log in or Sign up to answer