ScriptRunner
Incidents - Status
⚙ Technical Requirements
ScriptRunner for Jira Cloud installed
Script Runner Admin permission
Scripts use ScriptRunner Cloud JavaScript API
Deploy via ScriptRunner → Script Console or Listeners
ScriptRunner — 6 rules
RULE-001
Auto-close Completed Incidents after 7 days
To reduce manual effort and ensure a consistent lifecycle, this rule automatically closes incidents that have been in the 'Completed' status for 7 days without any updates.
Trigger
scheduled (0 0 1 * * ?)
Conditions
·An Incident has been in 'Completed' status for 7 or more days.
Actions
·Transition issue to 'Closed'
Script sr-cloud-js
const { issues, search } = require('@forge/jira');

const jql = `status = Completed AND updated <= '-7d' AND 'Work Type' = Incident`;
const searchResult = await search.findJiraIssues(jql);

for (const issue of searchResult.issues) {
  const issueKey = issue.key;
  console.log(`Processing issue: ${issueKey}`);

  const availableTransitions = await issues.getTransitions({ issueKey });
  const closeTransition = availableTransitions.transitions.find(t => t.to.name.toLowerCase() === 'closed');

  if (closeTransition) {
    await issues.transition({
      issueKey: issueKey,
      transition: { id: closeTransition.id }
    });
    console.log(`Successfully transitioned ${issueKey} to Closed.`);
  } else {
    console.error(`Could not find a transition to 'Closed' for issue ${issueKey}.`);
  }
}
RULE-002
Auto-assign Incident based on Subcategory
To speed up triage and assignment, this rule automatically assigns new incidents to the correct group based on their selected 'Subcategory'.
Trigger
listener
Conditions
·An Incident is created.
Actions
·Set 'Assignment Group' field based on 'Subcategory' value.
Script sr-cloud-js
const { fields } = require('@forge/jira');

// IMPORTANT: Replace these placeholder IDs with your actual custom field IDs.
const SUBCATEGORY_FIELD_ID = 'customfield_10100'; 
const ASSIGNMENT_GROUP_FIELD_ID = 'customfield_10101';

const issueKey = context.issue.key;

const subcategoryField = await fields.get({ issueKey, fieldId: SUBCATEGORY_FIELD_ID });
const subcategory = subcategoryField?.value;

if (!subcategory) {
  console.log(`No Subcategory set for ${issueKey}. Exiting.`);
  return;
}

const routingMap = {
  'Re-Image Laptop': 'Computer/Accessories',
  'Password Reset': 'Computer/Accessories',
  'PC': 'Computer/Accessories',
  'Access': 'Application Services',
  'Software Installation': 'Application Services',
  'Workday Issues/Request': 'HR - Workday',
  'System Integration': 'ERP Solutions',
  'Subscriptions': 'Mobile / Cell Phones'
};

const assignmentGroup = routingMap[subcategory];

if (assignmentGroup) {
  console.log(`Setting Assignment Group to '${assignmentGroup}' for ${issueKey}.`);
  await fields.set({
    issueKey,
    fieldId: ASSIGNMENT_GROUP_FIELD_ID,
    value: { value: assignmentGroup } // Assumes a single-select list field type
  });
}
RULE-003
Require Comment when Re-opening a Completed Incident
To improve process quality and traceability, this validator requires a user to add a comment explaining why a 'Completed' incident is being re-opened.
Trigger
validator
Conditions
·User attempts to transition an issue from 'Completed' to 'Work in Progress' or 'Pending'.
Actions
·Block transition if no comment is added, with the message: 'Please add a comment explaining why this incident is being re-opened.'
Script sr-cloud-js
// This script runs as a transition validator and must return a boolean value.
// 'true' allows the transition, 'false' blocks it.

const commentsAdded = context.issue.comments.added;

if (commentsAdded && commentsAdded.length > 0) {
  // A comment was added during the transition, so allow it.
  return true;
} else {
  // No comment was added, so block the transition.
  return false;
}
RULE-004
Escalate Stale Incidents in 'Pending' Status
To prevent incidents from being forgotten, this rule identifies incidents that have been in 'Pending' for more than 3 days and notifies the assignee to take action.
Trigger
scheduled (0 0 9 ? * MON-FRI)
Conditions
·An Incident has been in 'Pending' status for 3 or more days.
Actions
·Add a comment to the issue mentioning the assignee, requesting an update.
Script sr-cloud-js
const { search, issues } = require('@forge/jira');

const jql = `status = Pending AND updated <= '-3d' AND 'Work Type' = Incident`;
const searchResult = await search.findJiraIssues(jql);

for (const issue of searchResult.issues) {
  const issueKey = issue.key;
  const assignee = issue.fields.assignee;

  if (assignee && assignee.accountId) {
    const mention = `[~accountid:${assignee.accountId}]`;
    const commentBody = `${mention}, this incident has been in 'Pending' status for over 3 days. Please provide an update or progress the issue.`;
    
    console.log(`Adding escalation comment to ${issueKey}`);
    await issues.addComment({
      issueKey,
      body: commentBody
    });
  } else {
    console.log(`Issue ${issueKey} is unassigned, skipping comment.`);
  }
}
RULE-005
Auto-transition to 'Work in Progress' on Assignment
To streamline the workflow and accurately reflect when work begins, this rule automatically moves an incident from 'Open' to 'Work in Progress' as soon as it is assigned.
Trigger
listener
Conditions
·The 'Assignee' field is set for the first time on an 'Open' issue.
Actions
·Transition issue to 'Work in Progress'
Script sr-cloud-js
const { issues } = require('@forge/jira');

const issueKey = context.issue.key;
const changelog = context.changelog;

const assigneeChange = changelog.items.find(item => item.fieldId === 'assignee');

// Check if assignee was changed from unassigned to assigned
if (assigneeChange && !assigneeChange.from && assigneeChange.to) {
  console.log(`Issue ${issueKey} was assigned. Checking if it should be transitioned.`);

  const availableTransitions = await issues.getTransitions({ issueKey });
  const wipTransition = availableTransitions.transitions.find(t => t.to.name.toLowerCase() === 'work in progress');

  if (wipTransition) {
    console.log(`Transitioning ${issueKey} to 'Work in Progress'.`);
    await issues.transition({
      issueKey: issueKey,
      transition: { id: wipTransition.id }
    });
  } else {
    console.error(`Could not find a transition to 'Work in Progress' for issue ${issueKey}.`);
  }
}
RULE-006
Require 'Close Code' before moving to Completed
To ensure complete and accurate data for reporting, this validator prevents an incident from being moved to the 'Completed' status unless the 'Close Code' field has been filled out.
Trigger
validator
Conditions
·User attempts to transition an issue to the 'Completed' status.
Actions
·Block transition if 'Close Code' is empty, with the message: 'Please select a 'Close Code' before completing the incident.'
Script sr-cloud-js
// This script runs as a transition validator and must return a boolean value.
// 'true' allows the transition, 'false' blocks it.

// IMPORTANT: Replace this placeholder ID with your actual 'Close Code' custom field ID.
const CLOSE_CODE_FIELD_ID = 'customfield_10102';

// Get the value of the field from the transition screen
const closeCodeField = context.fields[CLOSE_CODE_FIELD_ID];

if (closeCodeField && closeCodeField.value) {
  // The field has a value, so allow the transition.
  return true;
} else {
  // The field is empty, so block the transition.
  return false;
}