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

IrmgardJaendl February 16, 2017

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
Answer accepted
adammarkham
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.
February 21, 2017

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.

0 votes
Andrea Gohlke February 22, 2017

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

IrmgardJaendl February 22, 2017

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

0 votes
IrmgardJaendl February 16, 2017

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...

Yves Riel [Okapya]
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 17, 2017

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.

IrmgardJaendl February 19, 2017

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() ...

Yves Riel [Okapya]
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 20, 2017

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.

JamieA
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.
February 21, 2017

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.

0 votes
Yves Riel [Okapya]
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 16, 2017

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events