Script runner plugin Validation script - Custom field ID instead of name

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 10, 2015

 

Hi ,

 

I am using Script runner plugin version 2.1.15, I have written a validation snippet which is working fine when I use the name of the custom field and not the ID, but I don't want my script to be tightly coupled with the custom field name

cfValues['Release'].getValue() == 'True

 

 

cfValues['customfield_12345'].getValue() == 'True'

is not, any suggestions?

2 answers

1 accepted

2 votes
Answer accepted
JamieA
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.
March 10, 2015

Look up the CF by ID...

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObject(12345)
return issue.getCustomFieldValue(cf)?.value == "True"
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2015

Hi @Jamie Echlin [Adaptavist] - My full statement involves multiple fields - issue.issueTypeObject.name != 'XYZ' || (issue.issueTypeObject.name == 'ABC' && cfValues['Release'].getValue() == 'True' && cfValues['myField'] && cfValues["myNewField''] && cfValues['MyFieldtwo']?.value?.size() > 0)

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2015

for other readers - Here "Release" is the select dropdown where as the "myfield", "mynewfield" are of type Text field thus if they are null groovy will interpret them as false.

JamieA
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.
March 11, 2015

I don't really see what the problem is... if you don't want to hard-code the name you need to hard-code the ID. Personally I prefer name because it's portable between instances. In your case you need to get multiple fields by multiple IDs.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2015

I actually hard-coded the ID using the cfValues['customfield_1234'] convention but that didn't work, i guess to make it work using the IDs is by using what you shared i.e. extracting the IDs using the customFieldManager

JamieA
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.
March 11, 2015

yes. cfValues is indexed only by *name*.

JamieA
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.
March 11, 2015

but it's just a convenience... if you don't want it then you should cfm to get the custom field by ID.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2015

thanks for sharing more info about it, as my requirement involved writing a long validation statement consiting of a certain scenario involving multiple custom fields and I wanted to restrict it to a single line of code but since cfValues doesn't work well with IDs thus would need to extract the IDs before hand.

JamieA
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.
March 11, 2015

no need to stick to a single line of code, write as much as you need to to make it clear

0 votes
SanaS
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.
March 10, 2015

What kind of custom field is your field? It will affect the way you retrieve the value from it.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 11, 2015

Hi @Sana Safai - Yes, ofcourse it would have an impact on the sytax based on the type of my custom field but I currently not concerned with that rather I am looking for a way to get the custom field values based on their ID instead of names in a long condition statement, see my comment in response to Jamie answer's. thanks

Suggest an answer

Log in or Sign up to answer