Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How add checklist template based on component selection?

Shah Baloch
Contributor
February 8, 2023

Hi Community, we recently started using "Checklist for Jira" from Okapya. I found a listener script from their documentation that can add a checklist template to an issue. I created a template and added the option "Account created", "Disk assigned" "Phone number assigned" "Laptop assigned" etc. I tested it's working fine. However, I would like to display the list of options based on component selection. For example, if a user selects the component "Onboarding" then should display a template else it should be an empty checklist where the user can create their own checklist option. How can add a condition to associate the template with a component?

import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.component.ComponentAccessor;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.okapya.jira.customfields.configuration.*;

def issue = event.issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def checklistField = customFieldManager.getCustomFieldObject("customfield_16200");
def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();
def templateId = 1;

Template template = checklistCFType.templatesManager.getTemplate(templateId);
Collection<ChecklistItem> templateItems = ChecklistSerializer.deserializeChecklist(template.getItemsJson());
Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue);
items.addAll(templateItems);
checklistCFType.updateValue(checklistField, issue, items);

Thank you for your help!

2 answers

1 accepted

1 vote
Answer accepted
Maxime Lefebvre _Okapya_
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 9, 2023

Hi Shah,

I am a developer from Okapya and will help you with adding a condition for your component.

Take a look at this example:

import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.component.ComponentAccessor;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.okapya.jira.customfields.configuration.*;

def issue = event.issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def checklistField = customFieldManager.getCustomFieldObject("customfield_16200");
def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();

Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue);

if ('MyComponent' in issue.components*.name) {
    def templateId = 1;
    Template template = checklistCFType.templatesManager.getTemplate(templateId);
    Collection<ChecklistItem> templateItems = ChecklistSerializer.deserializeChecklist(template.getItemsJson());
    items.addAll(templateItems);

}

checklistCFType.updateValue(checklistField, issue, items);

Make sure you change `MyComponent` to the component name you want.

Kind regards

Shah Baloch
Contributor
February 9, 2023

Thank you so much @Maxime Lefebvre _Okapya_ it worked.

Maxime Lefebvre _Okapya_
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 9, 2023

This is great to hear!

If you wanted to use Automation for Jira, we do have an "Import Template" action that would do it. Just make sure you've got version 6.2.0 or later installed.

Have a nice day!

Like Shah Baloch likes this
Shah Baloch
Contributor
February 9, 2023

Hi @Maxime Lefebvre _Okapya_ I forgot to ask. Is it possible to hide the checklist field for the rest of component just make it visible only for that component?

Thanks

Maxime Lefebvre _Okapya_
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 10, 2023

If you want the empty checklist to be hidden, you can disable the Show Checklist With No Items parameter. But this will only hide the checklist from the issue detail (once it's saved).

When the issue is opened in a dialog (Create or Edit dialog), the checklist will still be visible.

I think you can use ScriptRunner behaviors to also hide the Checklist field from the dialogs. Take a look at their explanation video.

I hope this helps!

Like Shah Baloch likes this
0 votes
Oleksandr Siryi_Railsware_
Atlassian Partner
February 9, 2023

Hi, @Shah Baloch 

I am using a different checklist add-on so not 100% sure this will be useful to you, but here's how I'd approach this. 

I'd try creating an automation rule. You can pick the "if/else block" for conditions and fill in Field with the "Components" value, select Condition and Value. 

Select "Edit issue" for action and choose the "Checklists" field and fill in the value that needs to be added as a checklist to the created request based on the selected component. Do the same for the 'Else" block. 

image.png

image (1).png

This example comes from a cloud Jira instance and you'll likely want to use Automation for Jira on a Server instance.

Shah Baloch
Contributor
February 9, 2023

@Oleksandr Siryi_Railsware_we found Checklist for Jira better than others we tried. It didn't show in automation's action tab. Thank you

Oleksandr Siryi_Railsware_
Atlassian Partner
February 9, 2023

Ok, glad you found the solution)

Like Shah Baloch likes this

Suggest an answer

Log in or Sign up to answer