How can I check in a script (script runner) whether a option form a checklist (okapya) is checked or not?

I have created a custom field, type checklist. The checklist options should cause the creation of a subtask, depending on whether a option is checked or not.
I added a script runner workflow function to create a subtask and tried to make a condition checking the options...
Here is how my condition script looks like:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.okapya.jira.customFields

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def checklist = customFieldManager.getCustomFieldObjectByName("Unteraufgaben erstellen");

checklist.getValue(issue).each {
   if (it.getName() == "JIRA User erstellen"){
      return it.isChecked()
   }
}
return false

Already in the editor I get an Error  "unable to resolve class com.okapya.jira.customfields". The Okapya checklist add-on is installed and it works but when I try to use the classes in a script it doesn't work. 

Where can I find the Add-on librarys in JIRA I needed to import?

4 answers

1 accepted

1 vote

There are two issues here.

As previously said your import is wrong. It should be a specific class or all classes in a particular package. In the last case it will be:

import com.okapya.jira.customfields.*

To use classes from other plugins you will need to use the WithPlugin annotation as explained here. This basically makes classes from other plugins available to ScriptRunner.

You'll need to provide it with the correct add-on key as explained in the link above.

If you want to know what the API for the Checklist for JIRA plugin looks like then you'll need to ask that plugin vendor.

I'm not really used to ScriptRunner.

However, what it seems to me is that you are trying to import a package and not a class. I think that you should try to import

com.okapya.jira.customfields.ChecklistItem

In your script, checklist.getValue(issue) should return a list of ChecklistItem.

Thank you,
there has been an answer from you in "https://answers.atlassian.com/questions/14817161/how-can-i-add-or-remove-a-option-toform-the-todolist-via-a-groovy-script" where you have discribed the ChecklistItem Class. Based on this description I made my script.

I tried various import versions:
import com.okapya.jira.customfields.ChecklistItem
import com.atlassian.okapya.jira.checklist
import com.atlassian.jira.issue.customfields.ChecklistItem
...
But no import worked. That's why I tried to find out where are the import librarys...

I'm sorry, I don't know where the libraries are located after the add-on has been installed. However, did you try without importing? The script won't know the type of class and will cast it as an Object but I think that it should work.

Yes, I tried. But without importing I got the error "Static type checking - Cannot find matching method". Therefore I replaced it.getName() with it.toString(), because the toString method in the ChecklistItem Class is overridden. But I can't replace it.isChecked() ...

Maybe you should contact the scriptrunner vendor. The proposed approach used to work for a few customers. Maybe they changed something in a newer version and they would be able to help you more as I don't have any experience with scriptrunner. If you have any more questions about the Checklist API, it will be my pleasure to help you out.

You get the static type checking error because you have not specified WithPlugin, so it can't load the correct classes to check the types.

However it's safe to ignore - groovy is a dynamic language, so if the custom field value responds to isChecked() it will work. 

https://scriptrunner.adaptavist.com/latest/jira/ - tl'dr - just ignore the error.

Now, there are a few reasons I was on the wrong track. Without a condition the script runner workflow function created the subtask. But when I added my condition above, checking the checklist options, the workflow function ran never. So I suposed the "static type checking error" caused my problem and the checklist type and its methods are not known.

After you advised me to ignore the error I had to find out another way to solve my problem. After I added some comments to the issue during the condition check, I had to realize that the problem is totally different. I suposed, the condition check will finish after the first return statement, like in C#. But the check run throu the whole checklist options. So, as you can see in my condition script above, the condition result was always false, because the last statement returned false.

Here I show you the new script where I added some comments to the issue during the condition check to understand what happends. The return value is now set via a variable so that the condition works properly.

Skriptrunner.png

Oh, sorry, false login - the answer was actually mine.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

364 views 0 6
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