Writing a Groovy Expression for my Post Function to Change a Custom Field Value

I am trying to write a Groovy Expression for Set Field Value to constant or Groovy expression Function. I want it to change a group lookup custom field value based on what exists in another custom field. I cannot seem to get it to work correctly. I think it is my syntax that is off.


Here is what I have currently:

if(issue.get("customfield_18173") == "Desktop" || issue.get("customfield_18173"  == "Support" || issue.get("customfield_18593") == "Desktop" || issue.get("customfield_18593") == "Support"){
setCustomFieldValue("customfield_18594", "Help Desk");

2 answers

1 accepted

1 vote
Accepted answer

It all depends on where you are trying to use your Groovy expression. If it's during a transition, you should use the Set Field Value to Constant or Groovy Expression post-function, with "Help Desk" as the value (text) and just the test as the conditional Groovy expression:

issue.get("customfield_18173") == "Desktop" || issue.get("customfield_18173"  == "Support" || issue.get("customfield_18593") == "Desktop" || issue.get("customfield_18593") == "Support"

Well that's what I had initially and it worked, however, I will be doing this for over 50 different constant values and I was hoping I could do it all in one post function instead of 50 different ones. What is the point of the Groovy Expression option in place of a raw value?

In the Groovy expression of the Set Field Value post-function, you can put some complex logic (like a cascade of ifs, or switch statements). The limitation is that if you want to not change the field value under some condition, you'll actually need to return... the current value of the field (using issue.get of course).

So, you can do something like:

if(issue.get("customfield_18173") == "Desktop" || issue.get("customfield_18173"  == "Support" || issue.get("customfield_18593") == "Desktop" || issue.get("customfield_18593") == "Support")
  return "Help Desk";
if ([...])
  return "something else";
return "otherwise";

Oh wow, I guess I was just making it way more complicated than it needed to be. I will give this a shot.

This works! Thank you very much!

So I had this on a random transition to test and it works, but I want to have this be on creation. When I try to do the same exact thing on the create transition it does not work. Any idea what I am doing wrong?

Tarun Sapra Community Champion Aug 30, 2016

You have to put the post-function at the end of the list of post-functions in case of create transitions.

Thank you.

1 vote
Tarun Sapra Community Champion Aug 29, 2016

Your syntax is incorrect, you have to use customFieldManager and issuse.getCustomFieldValue() method.

Please see here - https://answers.atlassian.com/questions/188266

And once you have the value you just need to update the value based on if condition just like - https://answers.atlassian.com/questions/211505 

And this works within the groovy expression box on post function?

Tarun Sapra Community Champion Aug 29, 2016


Suggest an answer

Log in or Sign up to answer
Community showcase
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,172 views 5 10
Read article

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