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

Cole
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.
August 29, 2016

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
Answer accepted
David _old account_
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.
August 29, 2016

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"
Cole
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.
August 29, 2016

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?

David _old account_
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.
August 29, 2016

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";
Cole
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.
August 29, 2016

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

Cole
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.
August 29, 2016

This works! Thank you very much!

Cole
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.
August 30, 2016

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 30, 2016

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

Cole
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.
August 30, 2016

Thank you.

1 vote
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.
August 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 

Cole
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.
August 29, 2016

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

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.
August 29, 2016

Yes

Suggest an answer

Log in or Sign up to answer