Set Field Value to constant or Groovy expression - how to use custom fields in groovy expression

Mark Love
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.
April 16, 2014

Hi,

I've created a post function using "Set field valule to constant or Groovy expression" that sets a custom field to "issueObject.key". It works fine.

I now want to modify this post-function so that it uses conditional execution, based on the value of different custom field.

I've tried all sorts of groovy expressions, but I can't get it to work. I know the field value to test for, but nothing seems to work, regardless of the expression, e.g.

issue.get("Create New SVR?") == "11301"

issue.get("Create New SVR?") != "11301"

issueObject.get("Create New SVR?") == "11301"

issueObject.get("Create New SVR?") != "11301"

issue.cf[12406] == "11301"

issue.cf[12406] != "11301"

issueObject.cf[12406] == "11301"

issueObject.cf[12406] != "11301"

I can write expressions that work on regular fields (description, summary, etc) but I can't see how to write it for a custom field.

And all the groovy documentation I've read refers to writing groovy scripts (not simple expressions) which all start with importing different components (e.g. import com.atlassian.jira.ComponentManager; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField;)

Can anyone help me?

thanks,
Mark.

8 answers

1 accepted

0 votes
Answer accepted
Mark Love
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.
April 21, 2014

You're right - my mistake. The checkbox setting was irrelevant - I've changed it back and it still works fine. I've also put the !=null clause back in, and that works too. Bizarre.

Immediately prior to making this change I performed a reindex - perhaps it was this that got things working?

1 vote
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.
April 16, 2014

The value you get from issue.get("customfield_12406") depends on the custom field type. If it's a Select list field, it will be an Option object, on which you can get the String value using .getValue().

Note that the result will then be a String, so you should compare it with a String constant using the .equals() method.

For example:

issue.get("customfield_12406") != null && issue.get("customfield_12406").getValue().equalsIgnoreCase("yes")

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.
April 16, 2014

Also, you can look at the atlassian-jira.log file to see if you're getting Groovy errors (look for "com.innovalog.jmwe").

You can also do simple debugging by writing to the log file using the following type of code:

log.error("this is a test");

Mark Love
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.
April 16, 2014

Thanks so much, I really appreciate the help, but I've used exactly the code you provided (coincidentally it was a "yes" option in the select list I was testing for) and still nothing is happenning (regardless of whether the custom field value is "yes" or "no").

If I just leave the clause issue.get("customfield_12406") != null then the field gets set as expected.

I can't get access to the logs until our Ops guys are back in on tuesday (we're about to shut down for Easter), so will try that then. In the meantime, any more thoughts appreciated.

thanks again,

Mark.

1 vote
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.
April 16, 2014

Hi Mark,

you need to use the "internal" JIRA field name. In the case of custom fields, it looks like "customfield_12345" where 12345 is the field numerical ID you can see in the URL to the Edit Field screen.

I've update the documentation for post-functions, see the note at the top of the page for details.

0 votes
Nadim Jarjour December 3, 2014

Dear @David [Innovalog],

I already tried ,issue.get("customfield_12406").getValue().equalsIgnoreCase("yes"), as you suggested.

But it failed.

Thanks for ur help.

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.
December 3, 2014

Then you must call issue.get("customfield_12406").getValue().equalsIgnoreCase("yes")

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.
December 3, 2014

You can add log.error(issue.get("customfield_12406")); log.error(issue.get("customfield_12406").getValue()); and then look inside atlassian-jira.log for "innovalog". You should find log entries or type "ERROR" with the value of these expressions. This will tell you if your test is correct.

Nadim Jarjour December 3, 2014

Dear @David [Innovalog] , am getting this error in the logs. java.lang.NullPointerException at com.innovalog.jmwe.plugins.functions.TransitionIssueFunction.executeFunction(TransitionIssueFunction.java:48) at com.innovalog.jmwe.plugins.functions.AbstractPreserveChangesPostFunction.execute(AbstractPreserveChangesPostFunction.java:121)

Nadim Jarjour December 3, 2014

Dear @David [Innovalog] , Now after putting the below, log.error("nadim is here"); issue.get("customfield_10611").equalsIgnoreCase("Billing Adjustment"); am not getting any error in the logs. I put the post function as final post function after the creation and reindexing and fire created issue. Thanks

Nadim Jarjour December 3, 2014

What am trying to do is, based on a value of a custom field A, if it has the value of A, it will create a linked issue with the status open. if it has the value of B, it will create a linked issue with the status in progress. so i put this post function in the level of the linked issue to transit directly to the in progress status when this customfield has the value of B. It is now working, please advise.

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.
December 3, 2014

You are getting a Null Pointer Exception in your Groovy script. issue.get("customfield_12406") must be returning NULL. Also, what you are trying to do won't work, because you need to use the "Transition Issue" post-function on the newly created linked issue, not the Transition Linked Issue post-function, which would need to be used on the original ("parent") issue. And the Transition Issue function doesn't work during the Create transition. See https://innovalog.atlassian.net/browse/JMWE-271.

0 votes
Nadim Jarjour December 3, 2014

Hi All,

I have a similar case, in transition linked issues and i would like to add a condition based on a value of a custom field.

I tried issue.get("customfield_12406").equalsIgnoreCase("yes") since it helped MArk to solve his issue, but it didn't  work with me.

Any help please ?


 

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.
December 3, 2014

It depends on whether that issue is of type checkbox or some other type.

0 votes
Mark Love
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.
April 21, 2014

Success!!!

It seems that the problem was down to the "Copy only if not set" checkbox - I had ticked this, as I didn'ty want values to be overwritten, but it seems as if it was conflicting in some way with the conditional logic below. I unticked it, and changed the condition to the following, and everything now works as expected.

issue.get("customfield_12406").equalsIgnoreCase("yes")

Should the "Copy only if not set" checkbox be doing this?

Regardless, I'm happy!

Thanks for your help,
Mark.

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.
April 21, 2014

I'm glad it worked. But I'm surprised by your explanation, as there is no relationship (in the code) between those two options. I suspect the value would not have been set even without the condition. Probably because the destination field is never really empty (== null).

0 votes
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.
April 16, 2014

So you have a Select type custom field with two possible values ('yes' and 'no')? If so, I'm not sure why it doesn't work. Can you try:

issue.get("customfield_12406") != null && issue.get("customfield_12406").getValue() == "yes"

If you are also using JIRA Misc Custom Fields (another of our plugins, but this one's free), you could create a Calculated Text Field with the following formula:

<!-- @@Formula: issue.get("customfield_12406").getValue() -->

to see what value you actually get from the field.

Mark Love
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.
April 16, 2014

Thanks David, I've just tried that as well withou success. (Our value is "Yes") but otherwise the same.


I also tried negating the query and saying != "Yes" at the end, and testing both yes/no options from the drop down, without success.

We've got Misc Custom Fields, so I'll give that a try next week to see what's going on there. I can't spend any more time onthis tonight.


Thanks again for all your help.

Mark.

Mark Love
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.
April 21, 2014

Hi David,

I've added a calculated text field with the expression...

<!-- @@Formula: issue.get("customfield_12406") -->

...and it correctly mirrors "Yes" or "No" from the custom field.

I also tried the expression you suggested (below), which didn't work - the field didn't diaply, suggesting the formula was failing.

<!-- @@Formula: issue.get("customfield_12406").getValue() -->

I've now been back and change the conditional expression to the following, but still with no joy.

issue.get("customfield_12406") != null && issue.get("customfield_12406").equalsIgnoreCase("yes")

I also disable JIRA Behaviours, in case that was conflicting in some way, but it didn't make a difference.

I will hopefully get access to the logs today, and will let you know what they say.

thanks,
Mark.

0 votes
Mark Love
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.
April 16, 2014

Thanks both for your quick and helpful responses. But unfortauntely I still can't get the condition to work. I've checked the custom field id (and the value that corresponds to the "yes" pick list item) and tried updating the condition expression to :

issue.get("customfield_12406") == "11301"
or
issue.get("customfield_12406") == 11301

but it still doesn't work.

I've also tried

issue.cfValues["customfield_12406"] == "11301"
or
issue.cfValues["customfield_12406"] == 11301

Any more ideas? I've also tried doing a re-index before this step, but that didn't make a difference.

thanks in anticipation.
Mark.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events