ScriptRunner Groovy Condition Question - Custom Field Is Not Null

MAG-II September 5, 2019

Hello - 

I believe what I am trying to achieve in a ScriptRunner post function should be fairly straight forward. I am trying to have an action trigger in the post function if the custom field is not null. The custom field type is a text field. So far I have:

 

issue.get("customfield_11203") != null

 

This does not seem to work for me. Obviously I am very new to Groovy as well as scripting in general. I would really appreciate if anyone could point me in the right direction. Thanks!

3 answers

1 accepted

0 votes
Answer accepted
Ilya Turov
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.
September 6, 2019

You can use just

cfValues['My Field Name']?.value

 if you are using a Simple scripted condition

edit: didn't see you are using it in a postfunction condition, but syntax is the same.

also, there should be "show examples" button below the script window, try looking at them

MAG-II September 6, 2019

Right - the "Show Examples" button is how I got to trying out:

issue.get("customfield_11203") != null

 

The attached screen of your example is also giving me errors. The ScriptRunner version that I am running is 5.6.1.1-jira8. I apologize for not being up to speed with this, and I appreciate all of the patience.

 

User ID.PNG

Ilya Turov
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.
September 6, 2019

what version of scriptrunner are you using?

Ilya Turov
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.
September 6, 2019

I'm not sure they've changed the API that drastically, maybe try just 

cfValues['User ID']

guess your field is string type and it should be enough

MAG-II September 6, 2019

Ah well it does seem like I am getting somewhere with the syntax you laid out. I'm not getting any errors, however, the action is not happening on the transition.

Per the attachment, you can see that if this field is not null, then execute this transition (Coordination Completed).

Post Function.PNG

 

Coordination Completed is the transition that's available on the status that this post function transition takes the Issue (the transition is there).

At this point it seems like my problem lies outside of the code in the post function. Again, thank you for your help and patience.

Like Mike McNamara likes this
robert Mugabuhamye
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.
September 6, 2019

have you checked the logs of the post-function after executing the action? Is it possible to have a screenshot of your workflow? the text version?

MAG-II September 6, 2019

So the plot thickens when looking at the logs. This is what they say:

 

Time (on server): Fri Sep 06 2019 09:49:05 GMT-0400 (Eastern Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-09-06 09:49:05,014 ERROR [utils.WorkflowUtils]: In-depth checking of each condition follows to determine why the action was not allowed. 
2019-09-06 09:49:05,014 ERROR [utils.WorkflowUtils]: 
It seems that you have tried to perform a workflow operation (Coordination Completed) that is not valid for the current state of this issue (VTEST-3916). The likely cause is that somebody has changed the issue recently, please look at the issue history for details.

 

I have no explanation as to why any kind of conditions are being mentioned here, because no conditions for transition Coordination Completed are in place. To validate further, I manually selected transition Coordination Completed and the Issue did run through this transition.

Seems bizarre.

robert Mugabuhamye
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.
September 6, 2019

so that why it is good to check the logs ;). 
my guess is you are trying to fast-track between two status that doesn't have a link between them or you are not using the right transition :).

Without having a view on your workflow, it will be hard to help :)

MAG-II September 6, 2019

Hah logs indeed are a powerful. I was in fact pointing out the incorrect transition. I fixed that error, and the ScriptRunner post function works fantastically! Powerful stuff. Thank you very much for your responsiveness and patience with me.

robert Mugabuhamye
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.
September 6, 2019

my pleasure and welcome to the community.
don't forget to mark this as resolved.

0 votes
MAG-II September 5, 2019

Thanks for the response. I am getting tripped up with the last line you've written, 

def cFieldValue = issue.getCustomFieldValue(cField)
// here you can compare with null

 

Not sure what I am meant to do here. So far I have:

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_11203")
def cFieldValue = issue.getCustomFieldValue(cField)

issue.get("customfield_11203") != null

 

I do believe once I better understand the concept of defining all of this information maybe I will be able to understand better how to think about this conceptually. I am struggling at the moment though.

robert Mugabuhamye
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.
September 5, 2019

no problem @MAG-II 

 

the last line should be :

cFieldValue != null
MAG-II September 5, 2019

Really appreciate your time. I think I am almost there. You can see in the attached screen that I get an error after the def, on the "c"  

 

Script.Error.PNG

robert Mugabuhamye
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.
September 5, 2019

what you want to do is something like this

 

def issueManager = ComponentAccessor.getIssueManager()
// here you get the custom field manager def customFieldManager = ComponentAccessor.getCustomFieldManager()
//from the manager you get the field def cField = customFieldManager.getCustomFieldObject("customfield_id")
//then you get the value of the field def cFieldValue = issue.getCustomFieldValue(cField)
// here you can compare with null

if (cFieldValue != NULL){
// do something here
}

 
regards

Like Berenberg_io Team likes this
MAG-II September 5, 2019

Ah so maybe my problem is what I am trying to achieve, and how I am attempting to go about it.

The ScriptRunner post function I am using is "Fast-track transition an issue." The idea is that I want to skip the next status if customfield_11203 is not null.

I was thinking I could simply state via Groovy condition:

issue.get("customfield_11203") != null

 

Maybe such an action is more complicated than I initially thought.

0 votes
robert Mugabuhamye
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.
September 5, 2019

hello @MAG-II 

try something like this

def issueManager = ComponentAccessor.getIssueManager()
// here you get the custom field manager def customFieldManager = ComponentAccessor.getCustomFieldManager()
//from the manager you get the field def cField = customFieldManager.getCustomFieldObject("customfield_id")
//then you get the value of the field def cFieldValue = issue.getCustomFieldValue(cField)
// here you can compare with null  

Suggest an answer

Log in or Sign up to answer