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

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.

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

Yes

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

94 views 0 5
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