/
ScriptRunner for Jira

ScriptRunner for Jira

You might find Smart Checklist integration with ScripRunner useful for updating checklists using automated scripts. 

Basic Case

You can add checklists to your Jira Issues by pushing value to the custom field "Checklists" using the following script:

ScriptRunner
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.util.DefaultIssueChangeHolder def customFieldManager = ComponentAccessor.getCustomFieldManager() def issueManager = ComponentAccessor.getIssueManager() def issue = issueManager.getIssueObject("PROJ-7") def changeHolder = new DefaultIssueChangeHolder() def String myval = "- ToDo List\n+ Checked\nx Skipped\n~ In Progress\n# Another ToDo List\n- Unchecked\n> Quote line 1 https://rw.rw\\n> Quote line 2\n> Quote line 3\n" // a text field def textCf = customFieldManager.getCustomFieldObjectByName("Checklists") textCf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(textCf), myval),changeHolder)

 

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.event.type.EventDispatchOption; import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.util.DefaultIssueChangeHolder def customFieldManager = ComponentAccessor.customFieldManager def issueManager = ComponentAccessor.issueManager def issueService = ComponentAccessor.issueService def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser def issue = issueManager.getIssueObject("PROJ-7") def changeHolder = new DefaultIssueChangeHolder() def inputParameters = issueService.newIssueInputParameters() def myval = "- ToDo List\n+ Checked\nx Skipped\n~ In Progress\n# Another ToDo List\n- Unchecked\n> Quote line 1 https://rw.rw\\n> Quote line 2\n> Quote line 3\n" // a text field def textCf = customFieldManager.getCustomFieldObjectByName("Checklists") inputParameters.addCustomFieldValue(textCf.getId(), myval); def updateValidationResult = issueService.validateUpdate(loggedInUser, issue.getId(), inputParameters); if (updateValidationResult.isValid()) { def result = issueService.update(loggedInUser, updateValidationResult, EventDispatchOption.ISSUE_UPDATED, false); if (!result.isValid()) { return result.getErrorCollection().getErrors().toString(); } } else { return updateValidationResult.getErrorCollection().getErrorMessages(); }

Result

Update/append checklists with post function during a workflow status change

This could be achieved by updating Issue Property with the key com.railsware.SmartChecklist.checklist using REST API Endpoints https://scriptrunner.adaptavist.com/5.6.8/jira/rest-endpoints.html

This use case can also be met with the native Smart Checklist feature. See the details here: Modify checklists on a Workflow Transition

Set checklist depending on Custom Field Value after Issue Creation

If you want the specific Checklist appended to your newly created ticket depending on the other Custom field value - you can easily do it by adding Script Runner Post Function to your Create Ticket workflow transition.

Assume that you have a custom field named "Environment" with 2 values: Production/Staging. So you expect that while adding a new issue - you'll have a proper checklist assigned

Result

Implementation flow:

  1. Go To Workflow Editor

  2. Choose "Script Post Function" Script Runner

  3. Choose "Custom Script Post Function"

  4. Add inline Script

  5. Variables checklistProd and checklistStage are used for keeping the proper value of the checklist (in text format)

    ScriptRunner

    import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import org.apache.log4j.Level log.setLevel(Level.DEBUG) //Grab necessary Components def cfm = ComponentAccessor.getCustomFieldManager() def optionsManager = ComponentAccessor.getOptionsManager() def cfEnv = cfm.getCustomFieldObjectByName("Environment") def cfEnvtValue = issue.getCustomFieldValue(cfEnv) def cfEnvValueString = cfEnvtValue.toString() def changeHolder = new DefaultIssueChangeHolder() def checklistProd = "- ToDo List\n+ Checked\nx Skipped\n~ In Progress\n# Another ToDo List\n- Unchecked\n> Quote line 1 https://rw.rw\\n> Quote line 2\n> Quote line 3\n" def checklistStage = "- Uno\n+ Dos\nx Tres\n~ Quatro\n" log.info("checklistProd: " + checklistProd) log.info("checklistStage: " + checklistStage) log.info("Environment"+ cfEnvValueString) if (cfEnvValueString == "Production") { //Set custom text field def cfClient = cfm.getCustomFieldObjectByName("Checklists") issue.setCustomFieldValue(cfClient,checklistProd) } else if (cfEnvValueString == "Staging") { //Set custom text field def cfClient = cfm.getCustomFieldObjectByName("Checklists") issue.setCustomFieldValue(cfClient,checklistStage) }



  6. Save the script. IMPORTANT! Make the Post Function is placed as a #1 Step!

Customize checklist visibility by issue type/user role/user group

ScriptRunner can be used for hiding separate UI elements on your Jira issues depending on different conditions. This way, you can hide Smart Checklist visibility on your Jira instance for specific user groups/ roles/ issue types.

Implementation flow to hide the checklist for a specific issue type:

image-20250227-050914.png

This rule will hide checklist add-on from issue view for a specific issue type in all projects on the instance.

// Define the issue types you want to hide elements for def hiddenIssueTypes = ["Task"] // Change this list based on your needs def issueType = issue.getIssueType().getName() return !hiddenIssueTypes.contains(issueType)

 

To hide checklist add-on from issue view based on the issue type for a specific project on the instance use this script:

// Define the issue types and projects where the checklist should be hidden def hiddenIssueTypes = ["Task"] // Change this list based on your needs def applicableProjects = ["HT"] // Add project keys where the rule should apply def issueType = issue.getIssueType().getName() def projectKey = issue.getProjectObject().getKey() return !(hiddenIssueTypes.contains(issueType) && applicableProjects.contains(projectKey))

Implementation flow to hide the checklist for a specific user role:

image-20250222-082541.png
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.security.roles.ProjectRoleManager // Get the ProjectRoleManager instance def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager) // Get the roles of the current user for the current project def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser def project = ComponentAccessor.getProjectManager().getProjectObj(issue.getProjectId()) def userRoles = projectRoleManager.getProjectRoles(loggedInUser, project) // Check if the user has the "Administrators" role return userRoles.any { it.name == "Administrators"}

Related content