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

Tarun Sapra Community Champion Mar 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

Look up the CF by ID...

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObject(12345)
return issue.getCustomFieldValue(cf)?.value == "True"
Tarun Sapra Community Champion Mar 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 Champion Mar 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.

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 Champion Mar 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

yes. cfValues is indexed only by *name*.

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 Champion Mar 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.

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

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

Tarun Sapra Community Champion Mar 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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,269 views 14 20
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot