Apps documentation
Issue Templates for Jira
Cloud Server/Data Center
Issue Templates for Jira

Cloud

Server/Data Center

Version 8.x
Version 7.x
FAQ
Release notes
Articles & Videos
Last updated Apr 16, 2023

ScriptRunner

This section provides information on useful ScriptRunner solutions for Issue Templates.

Setting a template field as mandatory

  1. Navigate to Project settings>Workflows and edit the Create issue transition.
  2. Open the Validators tab and add the Simple scripted validator from ScriptRunner.
  3. In the validator’s configuration, fill in the following details:
  • Name
  • Condition
  • Error message that will display for users on the Create issue screen
  • Template field
  1. Add the groovy script.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager


CustomFieldManager cfm = ComponentAccessor.getCustomFieldManager();
CustomField tempalteCF = cfm.getCustomFieldObject(10112L);
log.error('[LOG] tempalteCF: ' + tempalteCF)

Map templateValue = issue.getCustomFieldValue(tempalteCF)
log.error('[LOG] templateValue: ' + templateValue)

boolean templateIsEmpty = templateValue.get("SELECTED_TEMPLATE") == null;

if (templateIsEmpty) {
    log.error("Tempalte is empty")
    return false;
}

log.error("Tempalte is not empty")
return true;

Warning

You need to replace 10112 in the line CustomField tempalteCF = cfm.getCustomFieldObject(10112L); with your Template field’s ID.

ScriptRunner
ScriptRunner

Conditional hiding and validation of the template field (using Behaviours)

  1. Find the template field’s ID

    ScriptRunner
    ScriptRunner

  2. In Behaviours input the ID of the field’s whole DOM container (see below)

    ScriptRunner
    ScriptRunner

import com.onresolve.jira.groovy.user.FormField

FormField templateVisibilityFormField = getFieldById("customfield_12800")
String optionName = templateVisibilityFormField.getValue();

FormField tempalteFormField = getFieldById("issue-templates-field");

if (optionName.equals("Two (hide template)")) {
    tempalteFormField.setHidden(true)
} else {
    tempalteFormField.setHidden(false)
}
  1. Open the Validators tab and add the Simple scripted validator ScriptRunner.

    ScriptRunner
    ScriptRunner

  2. As a condition, add the script below.

The code for this solution provides:

An algorithm for hiding a field (by Behaviours):

  • Visibility of the template field on the screen is based on the value input in the single select (12800) field.
  • If you insert value Two (hide template) in this single select (12800) field, the template field (10112) will be hidden.
  • If there’s a different value than Two (hide template) in the single select (12800) field, then the field will be visible.

An algorithm for validating the template field (by Simple scripted validator from ScriptRunner):

  • Value of the single select (12800) and template (10112) fields is considered.
  • If the value of the single select (12800) field is Two (hide template) and the template (10112) field is empty, then no validation error is raised - like in this validation case above.
  • If the single select (12800) field has a different value than Two (hide template), the template (10112) field will be mandatory - the validating script will raise an error when there is no selected template in the template (10112) field.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager


CustomFieldManager cfm = ComponentAccessor.getCustomFieldManager();
CustomField templateCF = cfm.getCustomFieldObject(10112L);
CustomField templateVisibilityCF = cfm.getCustomFieldObject(12800L)
log.error('templateCF: ' + templateCF)
log.error('templateVisibilityCF: ' + templateVisibilityCF)


Map templateValue = issue.getCustomFieldValue(templateCF)
log.error('templateValue: ' + templateValue)


String templateVisibilityValue = issue.getCustomFieldValue(templateVisibilityCF)
log.error('templateVisibilityValue: ' + templateVisibilityValue)


boolean shouldBeOptional = "Two (hide template)".equals(templateVisibilityValue);
boolean templateIsEmpty = templateValue.get("SELECTED_TEMPLATE") == null;


if (!shouldBeOptional && templateIsEmpty) {
    log.error("Template is empty")
    return false;
}


log.error("Template is not empty")
return true;

Groovy

This section provides information on Groovy integration

You can obtain the id of the used template in Groovy using provided service as on the code below.
All scripts have been tested on ScriptRunner 4.3.12.

Note

Remember to add the annotation @WithPlugin("com.intenso.jira.issue-templates") to use Issue Templates for Jira app.

import pl.intenso.it.utils.export.PublicTemplateSevice
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.component.ComponentAccessor
 
 
@WithPlugin("com.intenso.jira.issue-templates")
def publicTemplateSevice = ComponentAccessor.getOSGiComponentInstanceOfType(PublicTemplateSevice.class)
  
def templateIdByKey = publicTemplateSevice.getAppliedTemplateIdByIssueKey("IT-100");
def templateIdById = publicTemplateSevice.getAppliedTemplateIdByIssueId(10100L);

Below version 6.4.2

import pl.intenso.it.utils.export.AppliedTemplateSevice
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.component.ComponentAccessor
@WithPlugin("com.intenso.jira.issue-templates")
def appliedTemplateSevice = ComponentAccessor.getOSGiComponentInstanceOfType(AppliedTemplateSevice.class)
  
def templateIdByKey = appliedTemplateSevice.getAppliedTemplateIdByIssueKey("IT-100");
def templateIdById = appliedTemplateSevice.getAppliedTemplateIdByIssueId(10100L);

Apply template with Copy set of fields


import pl.intenso.it.utils.export.PublicTemplateSevice
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue;
 
@WithPlugin("com.intenso.jira.issue-templates")
def publicTemplateSevice = ComponentAccessor.getOSGiComponentInstanceOfType(PublicTemplateSevice.class)
   
MutableIssue issue = issue
publicTemplateSevice.applyTemplateToIssue("TEMP-4", issue);

Copy subtasks from template


import pl.intenso.it.utils.export.PublicTemplateSevice
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue;
 
@WithPlugin("com.intenso.jira.issue-templates")
def publicTemplateSevice = ComponentAccessor.getOSGiComponentInstanceOfType(PublicTemplateSevice.class)
   
MutableIssue issue = issue
publicTemplateSevice.copySubtasksFromTemplate("TEMP-4", issue, "", null); //Template, current issue, stages and extra due date value
ScriptRunner
ScriptRunner
Need help?

If you can’t find the answer you need in our documentation, raise a support request.