It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner Groovy Condition Question - Custom Field Is Not Null

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

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

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

what version of scriptrunner are you using?

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

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.

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?

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.

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 :)

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.

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

hello @Michael Andrew Gayheart 

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  

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.

no problem @Michael Andrew Gayheart 

 

the last line should be :

cFieldValue != null

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

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

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.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

377 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you